@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
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ziggs-flavored EffectHandler.
|
|
3
|
+
*
|
|
4
|
+
* Translates the Agent's pure Effect requests into concrete calls against
|
|
5
|
+
* Ziggs backend primitives (MessagesClient, ArtifactsClient, ScopeClient,
|
|
6
|
+
* taskService, agreementService, messageSender) and the OpenAI adapter. This
|
|
7
|
+
* is the only place where Agent intent meets backend reality.
|
|
8
|
+
*
|
|
9
|
+
* Lives in agent-sdk for now alongside `Agent`; destined for a separate
|
|
10
|
+
* `@ziggs-ai/agent-server` package once the SDK split is complete.
|
|
11
|
+
*/
|
|
12
|
+
import type {
|
|
13
|
+
Effect, EffectHandler,
|
|
14
|
+
ContextSnapshot, LlmMessage, LlmResponse, LlmToolSchema, ToolCallResult,
|
|
15
|
+
} from '../types.js';
|
|
16
|
+
import { OutboxBuffer } from './OutboxBuffer.js';
|
|
17
|
+
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
18
|
+
/** Minimal LLM adapter contract — OpenAIAdapter satisfies this. */
|
|
19
|
+
interface LlmAdapter {
|
|
20
|
+
chatMessages(messages: LlmMessage[], tools?: LlmToolSchema[], options?: Record<string, unknown>): Promise<LlmResponse>;
|
|
21
|
+
chatMessagesStream?(messages: LlmMessage[], onChunk: (text: string) => void): Promise<string>;
|
|
22
|
+
}
|
|
23
|
+
import type { TaskService } from './tasks/TaskService.js';
|
|
24
|
+
import type { AgreementService } from './agreements/AgreementService.js';
|
|
25
|
+
import type { ToolManager } from '../tools/ToolManager.js';
|
|
26
|
+
import type {
|
|
27
|
+
MessagesClient,
|
|
28
|
+
ArtifactsClient,
|
|
29
|
+
ScopeClient,
|
|
30
|
+
TelemetryClient,
|
|
31
|
+
} from '@ziggs-ai/api-client';
|
|
32
|
+
import {
|
|
33
|
+
buildLlmIngestPayload,
|
|
34
|
+
buildToolIngestPayload,
|
|
35
|
+
isTelemetryIngestEnabled,
|
|
36
|
+
sendTelemetry,
|
|
37
|
+
} from './telemetryIngest.js';
|
|
38
|
+
|
|
39
|
+
export interface ZiggsEffectDeps {
|
|
40
|
+
llm: LlmAdapter;
|
|
41
|
+
messagesClient: MessagesClient;
|
|
42
|
+
artifactsClient: ArtifactsClient;
|
|
43
|
+
scopeClient: ScopeClient;
|
|
44
|
+
taskService: TaskService;
|
|
45
|
+
agreementService: AgreementService;
|
|
46
|
+
messageSender: (text: string, receiverId: string, sessionId: string, messageId?: string) => Promise<unknown>;
|
|
47
|
+
/** Sends a streaming chunk to a client. Called per-token during stream-text effects. */
|
|
48
|
+
chunkSender?: (sessionId: string, receiverId: string, text: string, messageId: string) => void;
|
|
49
|
+
toolManager: ToolManager;
|
|
50
|
+
operatorKey: string;
|
|
51
|
+
agentId: string;
|
|
52
|
+
telemetryClient?: TelemetryClient;
|
|
53
|
+
/** When false, skips POST /agents/monitoring/ingest (default: env on). */
|
|
54
|
+
telemetryIngest?: boolean;
|
|
55
|
+
effectTap?: (effect: Effect, response: unknown) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const CONTEXT_CACHE_MAX = 256;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Compose a `ContextSnapshot` for a chatId by fanning out to scope + messages
|
|
62
|
+
* primitives. Replaces the single `/context/read` aggregator with parallel
|
|
63
|
+
* pulls. The shape stays the same so PromptBuilder / cognition code doesn't
|
|
64
|
+
* change.
|
|
65
|
+
*/
|
|
66
|
+
const annotateAgreement = (ag: Record<string, unknown>, agentId: string): Record<string, unknown> => {
|
|
67
|
+
const parties = (ag?.parties ?? {}) as { creator?: string; provider?: string; payer?: string; proposedTo?: string };
|
|
68
|
+
return {
|
|
69
|
+
...ag,
|
|
70
|
+
creatorIsYou: parties.creator === agentId,
|
|
71
|
+
providerIsYou: parties.provider === agentId,
|
|
72
|
+
payerIsYou: parties.payer === agentId,
|
|
73
|
+
proposedToIsYou: parties.proposedTo === agentId,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
async function buildSnapshot(
|
|
78
|
+
deps: ZiggsEffectDeps,
|
|
79
|
+
chatId: string,
|
|
80
|
+
agentId: string,
|
|
81
|
+
opts: { after?: string; limit?: number } = {},
|
|
82
|
+
): Promise<ContextSnapshot> {
|
|
83
|
+
const [scope, msgs, chatAgreements] = await Promise.all([
|
|
84
|
+
deps.scopeClient.get('chat', chatId).catch((err: unknown) => {
|
|
85
|
+
runtimeLog.warn('ZiggsEffectHandler', `scope fetch failed: ${(err as Error)?.message ?? err}`);
|
|
86
|
+
return null;
|
|
87
|
+
}),
|
|
88
|
+
deps.messagesClient
|
|
89
|
+
.list(chatId, { after: opts.after, limit: opts.limit })
|
|
90
|
+
.catch((err: unknown) => {
|
|
91
|
+
runtimeLog.warn('ZiggsEffectHandler', `messages fetch failed: ${(err as Error)?.message ?? err}`);
|
|
92
|
+
return { messages: [], latestSequence: null };
|
|
93
|
+
}),
|
|
94
|
+
deps.agreementService.getByChat(chatId).catch((err: unknown) => {
|
|
95
|
+
runtimeLog.warn('ZiggsEffectHandler', `agreements fetch failed: ${(err as Error)?.message ?? err}`);
|
|
96
|
+
return [] as unknown[];
|
|
97
|
+
}),
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
const counterparties = (scope?.accessible?.counterparties ?? []) as Array<{
|
|
101
|
+
id: string;
|
|
102
|
+
role: string;
|
|
103
|
+
principalType?: 'user' | 'agent';
|
|
104
|
+
}>;
|
|
105
|
+
|
|
106
|
+
const others = counterparties.filter((c) => c.id !== agentId);
|
|
107
|
+
const users = others
|
|
108
|
+
.filter((c) => c.principalType === 'user')
|
|
109
|
+
.map((c) => ({ userId: c.id, role: c.role }));
|
|
110
|
+
const agents = others
|
|
111
|
+
.filter((c) => c.principalType !== 'user') // 'agent' or missing → treat as agent
|
|
112
|
+
.map((c) => ({ agentId: c.id, role: c.role, isYou: false }));
|
|
113
|
+
agents.push({ agentId, role: 'self', isYou: true });
|
|
114
|
+
|
|
115
|
+
const agreements = (chatAgreements as Record<string, unknown>[]).map((ag) => annotateAgreement(ag, agentId));
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
history: msgs.messages as Record<string, unknown>[],
|
|
119
|
+
agreements,
|
|
120
|
+
agents,
|
|
121
|
+
users,
|
|
122
|
+
latestSequence: msgs.latestSequence,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const OP_ARTIFACT_MAX_CHARS = 900;
|
|
127
|
+
|
|
128
|
+
function clipForOperationArtifact(value: unknown): unknown {
|
|
129
|
+
if (value == null) return value;
|
|
130
|
+
if (typeof value === 'string') {
|
|
131
|
+
return value.length > OP_ARTIFACT_MAX_CHARS ? `${value.slice(0, OP_ARTIFACT_MAX_CHARS - 1)}…` : value;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const s = JSON.stringify(value);
|
|
135
|
+
if (s.length <= OP_ARTIFACT_MAX_CHARS) return value;
|
|
136
|
+
return `${s.slice(0, OP_ARTIFACT_MAX_CHARS - 1)}…`;
|
|
137
|
+
} catch {
|
|
138
|
+
return String(value).slice(0, OP_ARTIFACT_MAX_CHARS);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function previewToolArgs(args: Record<string, unknown>): Record<string, unknown> | undefined {
|
|
143
|
+
const keys = Object.keys(args);
|
|
144
|
+
if (keys.length === 0) return undefined;
|
|
145
|
+
const out: Record<string, unknown> = {};
|
|
146
|
+
for (let i = 0; i < keys.length && i < 16; i++) {
|
|
147
|
+
const k = keys[i];
|
|
148
|
+
out[k] = clipForOperationArtifact(args[k]);
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Chat timeline: `Message.tsx` parses `entryType: artifact`, `content_type: operation` with
|
|
155
|
+
* `operation_*:{json}` where json includes `state` and `tool`.
|
|
156
|
+
*/
|
|
157
|
+
async function emitToolOperationArtifact(
|
|
158
|
+
client: ArtifactsClient,
|
|
159
|
+
chatId: string,
|
|
160
|
+
phase: 'started' | 'completed' | 'error',
|
|
161
|
+
payload: Record<string, unknown>,
|
|
162
|
+
): Promise<void> {
|
|
163
|
+
const prefix =
|
|
164
|
+
phase === 'started' ? 'operation_started' : phase === 'completed' ? 'operation_completed' : 'operation_error';
|
|
165
|
+
const text = `${prefix}:${JSON.stringify(payload)}`;
|
|
166
|
+
try {
|
|
167
|
+
await client.write({
|
|
168
|
+
chatId,
|
|
169
|
+
text,
|
|
170
|
+
content_type: 'operation',
|
|
171
|
+
visibility: 'agent-private',
|
|
172
|
+
});
|
|
173
|
+
} catch (e: unknown) {
|
|
174
|
+
runtimeLog.warn(
|
|
175
|
+
'ZiggsEffectHandler',
|
|
176
|
+
`tool operation artifact (${phase}): ${(e as Error)?.message ?? String(e)}`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Build a single shared EffectHandler. The handler is stateful — it caches
|
|
183
|
+
* the latest ContextSnapshot per sessionId so tool-call effects can resolve
|
|
184
|
+
* session-coupled deps (the `agents` roster) without forcing the Agent to
|
|
185
|
+
* pass them through. Cache is bounded with simple LRU eviction (re-set on
|
|
186
|
+
* read promotes the entry; oldest entry drops when size exceeds the cap).
|
|
187
|
+
*/
|
|
188
|
+
export function createZiggsEffectHandler(deps: ZiggsEffectDeps): EffectHandler {
|
|
189
|
+
const contextCache = new Map<string, ContextSnapshot>();
|
|
190
|
+
const outbox = new OutboxBuffer();
|
|
191
|
+
|
|
192
|
+
const cachePut = (sessionId: string, snap: ContextSnapshot): void => {
|
|
193
|
+
if (contextCache.has(sessionId)) contextCache.delete(sessionId);
|
|
194
|
+
contextCache.set(sessionId, snap);
|
|
195
|
+
if (contextCache.size > CONTEXT_CACHE_MAX) {
|
|
196
|
+
const oldest = contextCache.keys().next().value;
|
|
197
|
+
if (oldest !== undefined) contextCache.delete(oldest);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const cacheGet = (sessionId: string): ContextSnapshot | undefined => {
|
|
201
|
+
const snap = contextCache.get(sessionId);
|
|
202
|
+
if (snap !== undefined) {
|
|
203
|
+
contextCache.delete(sessionId);
|
|
204
|
+
contextCache.set(sessionId, snap);
|
|
205
|
+
}
|
|
206
|
+
return snap;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const dispatch = async (effect: Effect): Promise<unknown> => {
|
|
210
|
+
switch (effect.kind) {
|
|
211
|
+
case 'read-context': {
|
|
212
|
+
// Composite: assembles ContextSnapshot from scope + messages.
|
|
213
|
+
// Each agent runtime still gets the same shape, but the backend has
|
|
214
|
+
// no `/context/read` — assembly happens here, where the agent's
|
|
215
|
+
// identity is in scope.
|
|
216
|
+
const snap = await buildSnapshot(deps, effect.sessionId, deps.agentId, {
|
|
217
|
+
limit: effect.opts?.maxMessages,
|
|
218
|
+
});
|
|
219
|
+
const me = (snap.agents as Array<{ isYou?: boolean; agentId?: string }>).find((a) => a?.isYou)?.agentId || null;
|
|
220
|
+
outbox.merge(effect.sessionId, snap.history as Record<string, unknown>[], me);
|
|
221
|
+
cachePut(effect.sessionId, snap);
|
|
222
|
+
return snap;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
case 'list-messages': {
|
|
226
|
+
return deps.messagesClient.list(effect.chatId, {
|
|
227
|
+
after: effect.after,
|
|
228
|
+
limit: effect.limit,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
case 'list-artifacts': {
|
|
233
|
+
return deps.artifactsClient.list(
|
|
234
|
+
{ chatId: effect.chatId, agreementId: effect.agreementId },
|
|
235
|
+
{ after: effect.after, limit: effect.limit },
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
case 'list-task-history': {
|
|
240
|
+
const url = new URL(
|
|
241
|
+
`${process.env.ZIGGS_BACKEND_URL ?? 'http://localhost:3000'}/tasks/${effect.taskId}/history`,
|
|
242
|
+
);
|
|
243
|
+
if (effect.after) url.searchParams.set('after', effect.after);
|
|
244
|
+
if (effect.limit != null) url.searchParams.set('limit', String(effect.limit));
|
|
245
|
+
const res = await fetch(url.toString(), {
|
|
246
|
+
headers: {
|
|
247
|
+
'content-type': 'application/json',
|
|
248
|
+
Authorization: `Bearer ${deps.operatorKey}`,
|
|
249
|
+
'X-Agent-Id': deps.agentId,
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
if (!res.ok) {
|
|
253
|
+
throw new Error(`list-task-history ${res.status} ${res.statusText}`);
|
|
254
|
+
}
|
|
255
|
+
return res.json();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
case 'get-scope': {
|
|
259
|
+
return deps.scopeClient.get(effect.via.kind, effect.via.id);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
case 'llm-call': {
|
|
263
|
+
const t0 = Date.now();
|
|
264
|
+
const r: LlmResponse = await deps.llm.chatMessages(effect.messages, effect.tools);
|
|
265
|
+
sendTelemetry(
|
|
266
|
+
deps.telemetryClient,
|
|
267
|
+
buildLlmIngestPayload(r, {
|
|
268
|
+
sessionId: effect.sessionId,
|
|
269
|
+
durationMs: Date.now() - t0,
|
|
270
|
+
runId: effect.runId,
|
|
271
|
+
stateId: effect.stateId,
|
|
272
|
+
}),
|
|
273
|
+
isTelemetryIngestEnabled(deps.telemetryIngest),
|
|
274
|
+
);
|
|
275
|
+
return r;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
case 'stream-text': {
|
|
279
|
+
const { sessionId, receiverId, messageId, messages } = effect;
|
|
280
|
+
let text = '';
|
|
281
|
+
if (deps.llm.chatMessagesStream) {
|
|
282
|
+
text = await deps.llm.chatMessagesStream(messages as LlmMessage[], (chunk) => {
|
|
283
|
+
deps.chunkSender?.(sessionId, receiverId, chunk, messageId);
|
|
284
|
+
});
|
|
285
|
+
} else {
|
|
286
|
+
const r = await deps.llm.chatMessages(messages as LlmMessage[]);
|
|
287
|
+
text = r.content ?? '';
|
|
288
|
+
if (text) deps.chunkSender?.(sessionId, receiverId, text, messageId);
|
|
289
|
+
}
|
|
290
|
+
return { text };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
case 'tool-call': {
|
|
294
|
+
const toolT0 = Date.now();
|
|
295
|
+
const cached = cacheGet(effect.sessionId);
|
|
296
|
+
const agents = (cached?.agents as unknown[]) || [];
|
|
297
|
+
const users = (cached?.users as unknown[]) || [];
|
|
298
|
+
const args = (effect.args as Record<string, unknown>) || {};
|
|
299
|
+
const argsPreview = previewToolArgs(args);
|
|
300
|
+
await emitToolOperationArtifact(deps.artifactsClient, effect.sessionId, 'started', {
|
|
301
|
+
state: 'started',
|
|
302
|
+
tool: effect.name,
|
|
303
|
+
...(argsPreview ? { args: argsPreview } : {}),
|
|
304
|
+
});
|
|
305
|
+
try {
|
|
306
|
+
const lastUser = (users as Array<{ userId?: string; id?: string }>).find(
|
|
307
|
+
(u) => u.userId || u.id,
|
|
308
|
+
);
|
|
309
|
+
const result = await deps.toolManager.executeTool(
|
|
310
|
+
effect.name,
|
|
311
|
+
args,
|
|
312
|
+
{
|
|
313
|
+
operatorKey: deps.operatorKey,
|
|
314
|
+
agentId: deps.agentId,
|
|
315
|
+
chatId: effect.sessionId,
|
|
316
|
+
taskService: deps.taskService,
|
|
317
|
+
agreementService: deps.agreementService,
|
|
318
|
+
sendMessage: deps.messageSender,
|
|
319
|
+
agents,
|
|
320
|
+
users,
|
|
321
|
+
senderId: effect.senderId ?? lastUser?.userId ?? lastUser?.id,
|
|
322
|
+
senderType: effect.senderType ?? (lastUser ? 'USER' : undefined),
|
|
323
|
+
memory: effect.memory,
|
|
324
|
+
turnScratch: {},
|
|
325
|
+
},
|
|
326
|
+
);
|
|
327
|
+
await emitToolOperationArtifact(deps.artifactsClient, effect.sessionId, 'completed', {
|
|
328
|
+
state: 'completed',
|
|
329
|
+
tool: effect.name,
|
|
330
|
+
result: clipForOperationArtifact(result),
|
|
331
|
+
});
|
|
332
|
+
sendTelemetry(
|
|
333
|
+
deps.telemetryClient,
|
|
334
|
+
buildToolIngestPayload({
|
|
335
|
+
sessionId: effect.sessionId,
|
|
336
|
+
tool: effect.name,
|
|
337
|
+
durationMs: Date.now() - toolT0,
|
|
338
|
+
ok: true,
|
|
339
|
+
runId: effect.runId,
|
|
340
|
+
stateId: effect.stateId,
|
|
341
|
+
}),
|
|
342
|
+
isTelemetryIngestEnabled(deps.telemetryIngest),
|
|
343
|
+
);
|
|
344
|
+
return { ok: true, result } satisfies ToolCallResult;
|
|
345
|
+
} catch (error: unknown) {
|
|
346
|
+
const errMsg = (error as Error)?.message ?? String(error);
|
|
347
|
+
runtimeLog.warn('ZiggsEffectHandler', `tool-call "${effect.name}" failed: ${errMsg}`);
|
|
348
|
+
await emitToolOperationArtifact(deps.artifactsClient, effect.sessionId, 'error', {
|
|
349
|
+
state: 'failed',
|
|
350
|
+
tool: effect.name,
|
|
351
|
+
error: errMsg,
|
|
352
|
+
});
|
|
353
|
+
sendTelemetry(
|
|
354
|
+
deps.telemetryClient,
|
|
355
|
+
buildToolIngestPayload({
|
|
356
|
+
sessionId: effect.sessionId,
|
|
357
|
+
tool: effect.name,
|
|
358
|
+
durationMs: Date.now() - toolT0,
|
|
359
|
+
ok: false,
|
|
360
|
+
runId: effect.runId,
|
|
361
|
+
stateId: effect.stateId,
|
|
362
|
+
}),
|
|
363
|
+
isTelemetryIngestEnabled(deps.telemetryIngest),
|
|
364
|
+
);
|
|
365
|
+
return { ok: false, error: errMsg } satisfies ToolCallResult;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
case 'send-message': {
|
|
370
|
+
await deps.messageSender(effect.text, effect.receiverId, effect.sessionId, effect.messageId);
|
|
371
|
+
outbox.track(effect.sessionId, { text: effect.text, receiverId: effect.receiverId });
|
|
372
|
+
return { ok: true };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
case 'record-event': {
|
|
376
|
+
// Routes through ArtifactsClient as an agent-private artifact —
|
|
377
|
+
// visible to this agent across turns, not to other chat parties.
|
|
378
|
+
try {
|
|
379
|
+
if (effect.entry.kind === 'thought' && typeof effect.entry.text === 'string') {
|
|
380
|
+
await deps.artifactsClient.recordThought(effect.sessionId, effect.entry.text);
|
|
381
|
+
} else {
|
|
382
|
+
await deps.artifactsClient.write({
|
|
383
|
+
chatId: effect.sessionId,
|
|
384
|
+
text: JSON.stringify(effect.entry),
|
|
385
|
+
content_type: String(effect.entry.kind ?? 'event'),
|
|
386
|
+
visibility: 'agent-private',
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
} catch (err: unknown) {
|
|
390
|
+
runtimeLog.warn('ZiggsEffectHandler', `record-event failed: ${(err as Error)?.message ?? String(err)}`);
|
|
391
|
+
}
|
|
392
|
+
return { ok: true };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
default: {
|
|
396
|
+
const exhaustive: never = effect;
|
|
397
|
+
throw new Error(`ZiggsEffectHandler: unknown effect kind ${(exhaustive as Record<string, unknown>).kind}`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const handler = async (effect: Effect): Promise<unknown> => {
|
|
403
|
+
const response = await dispatch(effect);
|
|
404
|
+
deps.effectTap?.(effect, response);
|
|
405
|
+
return response;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
return handler as EffectHandler;
|
|
409
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import {
|
|
2
|
+
proposeAgreement, delegateAgreement, respondToAgreement, counterAgreement,
|
|
3
|
+
getAgreementStatus, listAgreements, getMyAgreements, getAgreement,
|
|
4
|
+
publishOffer as publishOfferApi, createAgreement, revokeAgreement, getAgreementsByChat, linkAgreementToChat,
|
|
5
|
+
getChatsForAgreement, joinAgreement, linkArtifactToAgreement, getArtifactsForAgreement,
|
|
6
|
+
linkUserToAgreement, getUsersForAgreement, createContract,
|
|
7
|
+
OPEN_AGREEMENT_TARGET,
|
|
8
|
+
type Creds, type Agreement,
|
|
9
|
+
type ProposeAgreementData, type ProposeDirectInput, type ProposeBroadcastInput,
|
|
10
|
+
type PublishOfferPayload,
|
|
11
|
+
type DelegateAgreementData, type CounterAgreementData,
|
|
12
|
+
type CreateAgreementBody, type ListAgreementsFilters, type GetMyAgreementsFilters,
|
|
13
|
+
type ChatLinkType, type ArtifactLinkType, type UserRole, type CreateContractInput,
|
|
14
|
+
} from '@ziggs-ai/api-client';
|
|
15
|
+
|
|
16
|
+
export class AgreementService {
|
|
17
|
+
private creds: Creds;
|
|
18
|
+
|
|
19
|
+
constructor(operatorKey: string, agentId: string) {
|
|
20
|
+
if (!operatorKey) throw new Error('AgreementService: operatorKey is required');
|
|
21
|
+
if (!agentId) throw new Error('AgreementService: agentId is required (operator-token impersonation)');
|
|
22
|
+
this.creds = { operatorKey, agentId };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @deprecated Prefer {@link proposeDirect} or {@link proposeBroadcast}. */
|
|
26
|
+
contract(input: CreateContractInput): Promise<Agreement> {
|
|
27
|
+
return createContract(input, this.creds);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 1:1 proposal. Server defaults engagementKind to `service` when omitted. */
|
|
31
|
+
proposeDirect(payload: ProposeDirectInput): Promise<Agreement> {
|
|
32
|
+
return proposeAgreement(payload, this.creds);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Open buyer-broadcast (`proposedTo: "everyone"`). Server defaults engagementKind to `service`. */
|
|
36
|
+
proposeBroadcast(payload: ProposeBroadcastInput): Promise<Agreement> {
|
|
37
|
+
return proposeAgreement({ ...payload, proposedTo: OPEN_AGREEMENT_TARGET }, this.creds);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @deprecated Alias for {@link proposeDirect}. */
|
|
41
|
+
propose(payload: ProposeAgreementData): Promise<Agreement> {
|
|
42
|
+
return proposeAgreement(payload, this.creds);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Seller-broadcast marketplace offer (`parties.payer = everyone`). */
|
|
46
|
+
publishOffer(payload: PublishOfferPayload): Promise<Agreement> {
|
|
47
|
+
return publishOfferApi(payload, this.creds);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
delegate(payload: DelegateAgreementData): Promise<Agreement> {
|
|
51
|
+
return delegateAgreement(payload, this.creds);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
respond(agreementId: string, action: 'approve' | 'reject'): Promise<Agreement> {
|
|
55
|
+
return respondToAgreement(agreementId, action, this.creds);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
counter(agreementId: string, counter: CounterAgreementData): Promise<Agreement> {
|
|
59
|
+
return counterAgreement(agreementId, counter || {}, this.creds);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getStatus(agreementId: string): Promise<unknown | null> {
|
|
63
|
+
return getAgreementStatus(agreementId, this.creds);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
list(filters: ListAgreementsFilters = {}): Promise<Agreement[]> {
|
|
67
|
+
return listAgreements(filters, this.creds);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
mine(filters: GetMyAgreementsFilters = {}): Promise<Agreement[]> {
|
|
71
|
+
return getMyAgreements(filters, this.creds);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get(agreementId: string): Promise<Agreement | null> {
|
|
75
|
+
return getAgreement(agreementId, this.creds);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
create(body: CreateAgreementBody): Promise<{ ok: boolean; agreement: Agreement }> {
|
|
79
|
+
return createAgreement(body, this.creds);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
revoke(agreementId: string): Promise<{ ok: boolean; agreement: Agreement }> {
|
|
83
|
+
return revokeAgreement(agreementId, this.creds);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getByChat(chatId: string): Promise<unknown[]> {
|
|
87
|
+
return getAgreementsByChat(chatId, this.creds);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
linkToChat(agreementId: string, chatId: string, linkType: ChatLinkType = 'mention'): Promise<unknown | null> {
|
|
91
|
+
return linkAgreementToChat(agreementId, chatId, linkType, this.creds);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getChats(agreementId: string): Promise<unknown[]> {
|
|
95
|
+
return getChatsForAgreement(agreementId, this.creds);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
join(agreementId: string): Promise<{ chatId: string; agentId: string | null; isNew: boolean }> {
|
|
99
|
+
return joinAgreement(agreementId, this.creds);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
linkArtifact(agreementId: string, artifactId: string, linkType: ArtifactLinkType = 'produced'): Promise<unknown | null> {
|
|
103
|
+
return linkArtifactToAgreement(agreementId, artifactId, linkType, this.creds);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getArtifacts(agreementId: string): Promise<unknown[]> {
|
|
107
|
+
return getArtifactsForAgreement(agreementId, this.creds);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
linkUser(agreementId: string, userId: string, role: UserRole): Promise<unknown | null> {
|
|
111
|
+
return linkUserToAgreement(agreementId, userId, role, this.creds);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getUsers(agreementId: string): Promise<unknown[]> {
|
|
115
|
+
return getUsersForAgreement(agreementId, this.creds);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { createServer, type Server } from 'http';
|
|
2
|
+
import type { ConnectionPool } from './ConnectionPool.js';
|
|
3
|
+
import type { AgentHost } from '../AgentHost.js';
|
|
4
|
+
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
5
|
+
|
|
6
|
+
function readJsonBody(req: import('http').IncomingMessage): Promise<Record<string, unknown>> {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const chunks: Buffer[] = [];
|
|
9
|
+
req.on('data', (c) => chunks.push(c));
|
|
10
|
+
req.on('end', () => {
|
|
11
|
+
try {
|
|
12
|
+
const raw = Buffer.concat(chunks).toString('utf8');
|
|
13
|
+
resolve(raw ? (JSON.parse(raw) as Record<string, unknown>) : {});
|
|
14
|
+
} catch (err) {
|
|
15
|
+
reject(err);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
req.on('error', reject);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createHealthServer({
|
|
23
|
+
port = Number(process.env.PORT) || 8080,
|
|
24
|
+
label = 'agents',
|
|
25
|
+
pool,
|
|
26
|
+
}: {
|
|
27
|
+
port?: number;
|
|
28
|
+
label?: string;
|
|
29
|
+
pool?: ConnectionPool;
|
|
30
|
+
} = {}): Server {
|
|
31
|
+
const server = createServer(async (req, res) => {
|
|
32
|
+
const url = req.url ?? '/';
|
|
33
|
+
const wakeMatch = url.match(/^\/fleet-wake\/([^/?]+)/);
|
|
34
|
+
if (req.method === 'POST' && wakeMatch && pool) {
|
|
35
|
+
const agentId = decodeURIComponent(wakeMatch[1]);
|
|
36
|
+
try {
|
|
37
|
+
await pool.wake(agentId);
|
|
38
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
39
|
+
res.end(JSON.stringify({ ok: true, agentId, active: pool.listActive().includes(agentId) }));
|
|
40
|
+
} catch (err) {
|
|
41
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
42
|
+
res.end(JSON.stringify({ ok: false, error: (err as Error).message }));
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (req.method === 'POST' && url === '/fleet-deliver' && pool) {
|
|
48
|
+
try {
|
|
49
|
+
const body = await readJsonBody(req);
|
|
50
|
+
const agentId = String(body.agentId ?? '');
|
|
51
|
+
const chatId = String(body.chatId ?? '');
|
|
52
|
+
const text = String(body.text ?? '');
|
|
53
|
+
const senderId = String(body.senderId ?? '');
|
|
54
|
+
if (!agentId || !chatId || !text || !senderId) {
|
|
55
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
56
|
+
res.end(JSON.stringify({ ok: false, error: 'agentId, chatId, text, senderId required' }));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const host = (await pool.wake(agentId)) as AgentHost;
|
|
60
|
+
await host.handleMessage(text, {
|
|
61
|
+
chatId,
|
|
62
|
+
entryType: 'message',
|
|
63
|
+
content_type: 'text',
|
|
64
|
+
sender: { id: senderId, type: 'USER' },
|
|
65
|
+
receiver: { id: agentId, type: 'AGENT' },
|
|
66
|
+
});
|
|
67
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
68
|
+
res.end(JSON.stringify({ ok: true, agentId }));
|
|
69
|
+
} catch (err) {
|
|
70
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
71
|
+
res.end(JSON.stringify({ ok: false, error: (err as Error).message }));
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (url === '/health' || url === '/') {
|
|
76
|
+
res.writeHead(200);
|
|
77
|
+
res.end('ok');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
res.writeHead(404);
|
|
81
|
+
res.end();
|
|
82
|
+
});
|
|
83
|
+
server.listen(port, () => runtimeLog.info(label, `health server on port ${port}`));
|
|
84
|
+
return server;
|
|
85
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { publishOffer, pullOffers, claimOffer, type Creds } from '@ziggs-ai/api-client';
|
|
2
|
+
import type { PublishOfferPayload, PullOffersOptions } from '@ziggs-ai/api-client';
|
|
3
|
+
import { runtimeLog } from '../../shared/runtimeLog.js';
|
|
4
|
+
|
|
5
|
+
interface TickContext {
|
|
6
|
+
agentId: string;
|
|
7
|
+
marketplace: {
|
|
8
|
+
publishOffer: (payload: PublishOfferPayload) => Promise<unknown>;
|
|
9
|
+
pullOffers: (options?: PullOffersOptions) => Promise<unknown[]>;
|
|
10
|
+
claimOffer: (agreementId: string) => Promise<unknown>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ProactiveTriggerOptions {
|
|
15
|
+
interval: number;
|
|
16
|
+
creds: Creds;
|
|
17
|
+
onTick: (ctx: TickContext) => Promise<void>;
|
|
18
|
+
onError?: (err: unknown) => void;
|
|
19
|
+
runImmediately?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class ProactiveTrigger {
|
|
23
|
+
private interval: number;
|
|
24
|
+
private creds: Creds;
|
|
25
|
+
private onTick: (ctx: TickContext) => Promise<void>;
|
|
26
|
+
private onError: (err: unknown) => void;
|
|
27
|
+
private runImmediately: boolean;
|
|
28
|
+
private _running: boolean;
|
|
29
|
+
private _timer: ReturnType<typeof setTimeout> | null;
|
|
30
|
+
|
|
31
|
+
constructor({ interval, creds, onTick, onError, runImmediately = false }: ProactiveTriggerOptions) {
|
|
32
|
+
if (!interval || typeof interval !== 'number' || interval < 1000) throw new Error('ProactiveTrigger: interval must be a number >= 1000ms');
|
|
33
|
+
if (!creds?.operatorKey || !creds?.agentId) throw new Error('ProactiveTrigger: creds.operatorKey and creds.agentId are required');
|
|
34
|
+
if (typeof onTick !== 'function') throw new Error('ProactiveTrigger: onTick must be a function');
|
|
35
|
+
this.interval = interval;
|
|
36
|
+
this.creds = creds;
|
|
37
|
+
this.onTick = onTick;
|
|
38
|
+
this.onError = onError ?? ((err) => runtimeLog.error('ProactiveTrigger', `tick error: ${String(err)}`));
|
|
39
|
+
this.runImmediately = runImmediately;
|
|
40
|
+
this._running = false;
|
|
41
|
+
this._timer = null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
start(): void {
|
|
45
|
+
if (this._running) return;
|
|
46
|
+
this._running = true;
|
|
47
|
+
runtimeLog.debug(
|
|
48
|
+
'ProactiveTrigger',
|
|
49
|
+
`started (agentId=${this.creds.agentId}, interval=${this.interval}ms)`,
|
|
50
|
+
);
|
|
51
|
+
if (this.runImmediately) this._tick();
|
|
52
|
+
else this._schedule();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
stop(): void {
|
|
56
|
+
this._running = false;
|
|
57
|
+
if (this._timer) { clearTimeout(this._timer); this._timer = null; }
|
|
58
|
+
runtimeLog.debug('ProactiveTrigger', `stopped (agentId=${this.creds.agentId})`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private _schedule(): void {
|
|
62
|
+
if (!this._running) return;
|
|
63
|
+
this._timer = setTimeout(() => this._tick(), this.interval);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private async _tick(): Promise<void> {
|
|
67
|
+
if (!this._running) return;
|
|
68
|
+
try { await this.onTick(this._buildContext()); } catch (err) { this.onError(err); }
|
|
69
|
+
this._schedule();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private _buildContext(): TickContext {
|
|
73
|
+
const creds = this.creds;
|
|
74
|
+
return {
|
|
75
|
+
agentId: creds.agentId,
|
|
76
|
+
marketplace: {
|
|
77
|
+
publishOffer: (payload) => publishOffer(payload, creds),
|
|
78
|
+
pullOffers: (options) => pullOffers(options, creds),
|
|
79
|
+
claimOffer: (agreementId) => claimOffer(agreementId, creds),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|