@compose-market/sdk 0.2.0 → 0.4.0
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/CHANGELOG.md +54 -9
- package/dist/events.d.ts +115 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +68 -0
- package/dist/events.js.map +1 -0
- package/dist/http.d.ts +1 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +4 -4
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +122 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +255 -18
- package/dist/index.js.map +1 -1
- package/dist/resources/agent.d.ts +43 -0
- package/dist/resources/agent.d.ts.map +1 -0
- package/dist/resources/agent.js +297 -0
- package/dist/resources/agent.js.map +1 -0
- package/dist/resources/inference.d.ts +34 -15
- package/dist/resources/inference.d.ts.map +1 -1
- package/dist/resources/inference.js +200 -55
- package/dist/resources/inference.js.map +1 -1
- package/dist/resources/instrumentation.d.ts +28 -0
- package/dist/resources/instrumentation.d.ts.map +1 -0
- package/dist/resources/instrumentation.js +43 -0
- package/dist/resources/instrumentation.js.map +1 -0
- package/dist/resources/keys.d.ts +4 -0
- package/dist/resources/keys.d.ts.map +1 -1
- package/dist/resources/keys.js +15 -5
- package/dist/resources/keys.js.map +1 -1
- package/dist/resources/session-events.d.ts +46 -0
- package/dist/resources/session-events.d.ts.map +1 -0
- package/dist/resources/session-events.js +199 -0
- package/dist/resources/session-events.js.map +1 -0
- package/dist/resources/workflow.d.ts +47 -0
- package/dist/resources/workflow.d.ts.map +1 -0
- package/dist/resources/workflow.js +321 -0
- package/dist/resources/workflow.js.map +1 -0
- package/dist/storage.d.ts +27 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +46 -0
- package/dist/storage.js.map +1 -0
- package/dist/streaming/budget.d.ts +22 -0
- package/dist/streaming/budget.d.ts.map +1 -0
- package/dist/streaming/budget.js +40 -0
- package/dist/streaming/budget.js.map +1 -0
- package/dist/types/index.d.ts +165 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/version.d.ts +9 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +9 -0
- package/dist/version.js.map +1 -0
- package/package.json +6 -5
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compose workflow runtime stream resource.
|
|
3
|
+
*
|
|
4
|
+
* Subscribes to POST /workflow/:wallet/chat on the Compose runtime and yields
|
|
5
|
+
* typed `WorkflowRuntimeEvent` values. The workflow orchestrator emits its
|
|
6
|
+
* own vocabulary (`start`, `step`, `agent`, `progress`, `tool_start`,
|
|
7
|
+
* `tool_end`, `result`, `error`, `complete`, `done`) via named SSE events;
|
|
8
|
+
* each frame carries a JSON payload with an optional `message` field.
|
|
9
|
+
*
|
|
10
|
+
* Same normalisation contract as `AgentResource`:
|
|
11
|
+
* - Tool lifecycle fires on `sdk.events.toolCallStart` / `toolCallEnd`.
|
|
12
|
+
* - Workflow-level start/end fires on `workflowStreamStart` / `workflowStreamEnd`.
|
|
13
|
+
* - Budget / receipt / sessionInvalid come from response headers + streamed
|
|
14
|
+
* `compose.receipt` frames.
|
|
15
|
+
*/
|
|
16
|
+
import { ComposeError } from "../errors.js";
|
|
17
|
+
import { parseSSEStream } from "../streaming/sse.js";
|
|
18
|
+
import { extractReceiptFromResponse, parseReceiptEvent } from "../streaming/receipt.js";
|
|
19
|
+
import { extractSessionBudgetFromResponse } from "../streaming/budget.js";
|
|
20
|
+
import { ComposeStreamIterator } from "./inference.js";
|
|
21
|
+
export class WorkflowResource {
|
|
22
|
+
ctx;
|
|
23
|
+
constructor(ctx) {
|
|
24
|
+
this.ctx = ctx;
|
|
25
|
+
}
|
|
26
|
+
stream(params, options = {}) {
|
|
27
|
+
return new ComposeStreamIterator(driveWorkflowStream(this.ctx, params, options));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fire-and-forget stop signal for an in-flight workflow execution.
|
|
31
|
+
* Returns the raw Response so callers can inspect status.
|
|
32
|
+
*/
|
|
33
|
+
async stop(workflowWallet, threadId, options = {}) {
|
|
34
|
+
const url = `${this.ctx.runtimeBaseUrl}/workflow/${encodeURIComponent(workflowWallet)}/stop`;
|
|
35
|
+
const wallet = this.ctx.getWalletMaybe();
|
|
36
|
+
const token = this.ctx.getTokenMaybe();
|
|
37
|
+
const headers = new Headers({
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
"User-Agent": this.ctx.userAgent,
|
|
40
|
+
});
|
|
41
|
+
if (token)
|
|
42
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
43
|
+
if (wallet.address)
|
|
44
|
+
headers.set("x-session-user-address", wallet.address);
|
|
45
|
+
if (wallet.chainId !== null)
|
|
46
|
+
headers.set("x-chain-id", String(wallet.chainId));
|
|
47
|
+
return this.ctx.fetch(url, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers,
|
|
50
|
+
body: JSON.stringify({ threadId }),
|
|
51
|
+
signal: options.signal,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function* driveWorkflowStream(ctx, params, options) {
|
|
56
|
+
const url = `${ctx.runtimeBaseUrl}/workflow/${encodeURIComponent(params.workflowWallet)}/chat`;
|
|
57
|
+
const wallet = ctx.getWalletMaybe();
|
|
58
|
+
const token = ctx.getTokenMaybe();
|
|
59
|
+
const headers = new Headers({
|
|
60
|
+
"Content-Type": "application/json",
|
|
61
|
+
"User-Agent": ctx.userAgent,
|
|
62
|
+
});
|
|
63
|
+
if (token)
|
|
64
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
65
|
+
if (wallet.address)
|
|
66
|
+
headers.set("x-session-user-address", wallet.address);
|
|
67
|
+
if (wallet.chainId !== null)
|
|
68
|
+
headers.set("x-chain-id", String(wallet.chainId));
|
|
69
|
+
if (params.composeRunId)
|
|
70
|
+
headers.set("x-compose-run-id", params.composeRunId);
|
|
71
|
+
const body = {
|
|
72
|
+
message: params.message,
|
|
73
|
+
threadId: params.threadId,
|
|
74
|
+
userAddress: params.userAddress,
|
|
75
|
+
};
|
|
76
|
+
if (params.composeRunId)
|
|
77
|
+
body.composeRunId = params.composeRunId;
|
|
78
|
+
if (typeof params.lastEventIndex === "number")
|
|
79
|
+
body.lastEventIndex = params.lastEventIndex;
|
|
80
|
+
if (typeof params.continuous === "boolean")
|
|
81
|
+
body.continuous = params.continuous;
|
|
82
|
+
if (params.attachment)
|
|
83
|
+
body.attachment = params.attachment;
|
|
84
|
+
const timeoutController = new AbortController();
|
|
85
|
+
const timeoutMs = options.timeoutMs ?? 10 * 60 * 1000;
|
|
86
|
+
const timer = setTimeout(() => timeoutController.abort(), timeoutMs);
|
|
87
|
+
ctx.events.emit("workflowStreamStart", {
|
|
88
|
+
userAddress: wallet.address,
|
|
89
|
+
chainId: wallet.chainId,
|
|
90
|
+
requestId: null,
|
|
91
|
+
workflowWallet: params.workflowWallet,
|
|
92
|
+
threadId: params.threadId,
|
|
93
|
+
runId: params.composeRunId,
|
|
94
|
+
});
|
|
95
|
+
let response;
|
|
96
|
+
try {
|
|
97
|
+
response = await ctx.fetch(url, {
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers,
|
|
100
|
+
body: JSON.stringify(body),
|
|
101
|
+
signal: mergeSignals(options.signal, timeoutController.signal),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (fetchError) {
|
|
105
|
+
clearTimeout(timer);
|
|
106
|
+
throw new ComposeError({
|
|
107
|
+
code: "network_error",
|
|
108
|
+
message: fetchError instanceof Error ? fetchError.message : String(fetchError),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
if (!response.ok) {
|
|
113
|
+
const text = await response.text().catch(() => "");
|
|
114
|
+
throw new ComposeError({
|
|
115
|
+
code: "upstream_error",
|
|
116
|
+
message: `Workflow runtime returned ${response.status}: ${text.slice(0, 500)}`,
|
|
117
|
+
status: response.status,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (!response.body) {
|
|
121
|
+
throw new ComposeError({ code: "upstream_error", message: "Workflow runtime stream had no body" });
|
|
122
|
+
}
|
|
123
|
+
const requestId = response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id");
|
|
124
|
+
const headerReceipt = extractReceiptFromResponse(response);
|
|
125
|
+
const { budget, sessionInvalidReason } = extractSessionBudgetFromResponse(response);
|
|
126
|
+
if (headerReceipt) {
|
|
127
|
+
ctx.events.emit("receipt", {
|
|
128
|
+
userAddress: wallet.address,
|
|
129
|
+
chainId: wallet.chainId,
|
|
130
|
+
receipt: headerReceipt,
|
|
131
|
+
requestId,
|
|
132
|
+
source: "response-header",
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (budget) {
|
|
136
|
+
ctx.events.emit("budget", {
|
|
137
|
+
userAddress: wallet.address,
|
|
138
|
+
chainId: wallet.chainId,
|
|
139
|
+
snapshot: budget,
|
|
140
|
+
requestId,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (sessionInvalidReason) {
|
|
144
|
+
ctx.events.emit("sessionInvalid", {
|
|
145
|
+
userAddress: wallet.address,
|
|
146
|
+
chainId: wallet.chainId,
|
|
147
|
+
reason: sessionInvalidReason,
|
|
148
|
+
requestId,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
let text = "";
|
|
152
|
+
let structuredOutput = null;
|
|
153
|
+
let streamReceipt = null;
|
|
154
|
+
const toolCalls = [];
|
|
155
|
+
try {
|
|
156
|
+
for await (const frame of parseSSEStream(response.body, { signal: options.signal })) {
|
|
157
|
+
const data = frame.data.trim();
|
|
158
|
+
if (!data || data === "[DONE]") {
|
|
159
|
+
if (data === "[DONE]")
|
|
160
|
+
yield { type: "done" };
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (frame.event === "compose.receipt") {
|
|
164
|
+
try {
|
|
165
|
+
streamReceipt = parseReceiptEvent(frame.data);
|
|
166
|
+
ctx.events.emit("receipt", {
|
|
167
|
+
userAddress: wallet.address,
|
|
168
|
+
chainId: wallet.chainId,
|
|
169
|
+
receipt: streamReceipt,
|
|
170
|
+
requestId,
|
|
171
|
+
source: "stream",
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
catch { /* skip malformed */ }
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
let payload;
|
|
178
|
+
try {
|
|
179
|
+
payload = JSON.parse(data);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const eventName = frame.event || "";
|
|
185
|
+
if (eventName === "start") {
|
|
186
|
+
yield { type: "start", message: asString(payload.message, "Starting workflow..."), meta: payload };
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (eventName === "step") {
|
|
190
|
+
yield {
|
|
191
|
+
type: "step",
|
|
192
|
+
stepName: typeof payload.stepName === "string" ? payload.stepName : undefined,
|
|
193
|
+
message: asString(payload.message, `Processing ${asString(payload.stepName, "workflow step")}...`),
|
|
194
|
+
meta: payload,
|
|
195
|
+
};
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (eventName === "agent") {
|
|
199
|
+
yield {
|
|
200
|
+
type: "agent",
|
|
201
|
+
agentName: typeof payload.agentName === "string" ? payload.agentName : undefined,
|
|
202
|
+
message: asString(payload.message, `Processing ${asString(payload.agentName, "agent")}...`),
|
|
203
|
+
meta: payload,
|
|
204
|
+
};
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (eventName === "progress") {
|
|
208
|
+
yield { type: "progress", message: asString(payload.message, "Running workflow..."), meta: payload };
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (eventName === "tool_start") {
|
|
212
|
+
const toolName = asString(payload.toolName, "tool");
|
|
213
|
+
const summary = typeof payload.content === "string"
|
|
214
|
+
? payload.content
|
|
215
|
+
: typeof payload.message === "string" ? payload.message : undefined;
|
|
216
|
+
const content = typeof payload.content === "string" ? payload.content : undefined;
|
|
217
|
+
ctx.events.emit("toolCallStart", {
|
|
218
|
+
userAddress: wallet.address,
|
|
219
|
+
chainId: wallet.chainId,
|
|
220
|
+
requestId,
|
|
221
|
+
source: "workflow",
|
|
222
|
+
toolCallId: toolName,
|
|
223
|
+
toolName,
|
|
224
|
+
summary,
|
|
225
|
+
});
|
|
226
|
+
yield { type: "tool-start", toolName, summary, content };
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (eventName === "tool_end") {
|
|
230
|
+
const toolName = asString(payload.toolName, "tool");
|
|
231
|
+
const summary = typeof payload.message === "string" ? payload.message : undefined;
|
|
232
|
+
const failed = typeof payload.error === "string" && payload.error.length > 0;
|
|
233
|
+
const error = failed ? payload.error : undefined;
|
|
234
|
+
toolCalls.push({ toolName, summary, failed, error });
|
|
235
|
+
ctx.events.emit("toolCallEnd", {
|
|
236
|
+
userAddress: wallet.address,
|
|
237
|
+
chainId: wallet.chainId,
|
|
238
|
+
requestId,
|
|
239
|
+
source: "workflow",
|
|
240
|
+
toolCallId: toolName,
|
|
241
|
+
toolName,
|
|
242
|
+
summary,
|
|
243
|
+
failed,
|
|
244
|
+
error,
|
|
245
|
+
});
|
|
246
|
+
yield { type: "tool-end", toolName, summary, failed, error };
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (eventName === "result") {
|
|
250
|
+
const output = payload.output;
|
|
251
|
+
structuredOutput = output;
|
|
252
|
+
const coerced = typeof output === "string"
|
|
253
|
+
? output
|
|
254
|
+
: typeof output === "object" && output !== null
|
|
255
|
+
? JSON.stringify(output)
|
|
256
|
+
: "";
|
|
257
|
+
if (coerced)
|
|
258
|
+
text = coerced;
|
|
259
|
+
yield { type: "result", output };
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (eventName === "error") {
|
|
263
|
+
const code = typeof payload.code === "string" ? payload.code : undefined;
|
|
264
|
+
const message = typeof payload.error === "string"
|
|
265
|
+
? payload.error
|
|
266
|
+
: typeof payload.message === "string"
|
|
267
|
+
? payload.message
|
|
268
|
+
: "Workflow stream error";
|
|
269
|
+
yield { type: "error", code, message };
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (eventName === "complete") {
|
|
273
|
+
yield { type: "complete", message: asString(payload.message, "Workflow complete!") };
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (eventName === "done") {
|
|
277
|
+
yield { type: "done" };
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
// Unknown runtime frame — ignore rather than crash.
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
try {
|
|
285
|
+
response.body?.cancel();
|
|
286
|
+
}
|
|
287
|
+
catch { /* best-effort */ }
|
|
288
|
+
ctx.events.emit("workflowStreamEnd", {
|
|
289
|
+
userAddress: wallet.address,
|
|
290
|
+
chainId: wallet.chainId,
|
|
291
|
+
requestId,
|
|
292
|
+
workflowWallet: params.workflowWallet,
|
|
293
|
+
threadId: params.threadId,
|
|
294
|
+
runId: params.composeRunId,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
text,
|
|
299
|
+
structuredOutput,
|
|
300
|
+
toolCalls,
|
|
301
|
+
requestId,
|
|
302
|
+
receipt: streamReceipt ?? headerReceipt,
|
|
303
|
+
budget,
|
|
304
|
+
sessionInvalidReason,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function asString(value, fallback) {
|
|
308
|
+
return typeof value === "string" && value.length > 0 ? value : fallback;
|
|
309
|
+
}
|
|
310
|
+
function mergeSignals(a, b) {
|
|
311
|
+
if (!a)
|
|
312
|
+
return b;
|
|
313
|
+
const c = new AbortController();
|
|
314
|
+
const forward = () => c.abort();
|
|
315
|
+
if (a.aborted || b.aborted)
|
|
316
|
+
c.abort();
|
|
317
|
+
a.addEventListener("abort", forward, { once: true });
|
|
318
|
+
b.addEventListener("abort", forward, { once: true });
|
|
319
|
+
return c.signal;
|
|
320
|
+
}
|
|
321
|
+
//# sourceMappingURL=workflow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workflow.js","sourceRoot":"","sources":["../../src/resources/workflow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACxF,OAAO,EAAE,gCAAgC,EAAE,MAAM,wBAAwB,CAAC;AAQ1E,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAWvD,MAAM,OAAO,gBAAgB;IACI;IAA7B,YAA6B,GAA4B;QAA5B,QAAG,GAAH,GAAG,CAAyB;IAAG,CAAC;IAE7D,MAAM,CACF,MAAkC,EAClC,UAAwD,EAAE;QAE1D,OAAO,IAAI,qBAAqB,CAAC,mBAAmB,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACrF,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,IAAI,CAAC,cAAsB,EAAE,QAAgB,EAAE,UAAoC,EAAE;QACvF,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,aAAa,kBAAkB,CAAC,cAAc,CAAC,OAAO,CAAC;QAC7F,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;QACzC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,CAAC;QACvC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC;YACxB,cAAc,EAAE,kBAAkB;YAClC,YAAY,EAAE,IAAI,CAAC,GAAG,CAAC,SAAS;SACnC,CAAC,CAAC;QACH,IAAI,KAAK;YAAE,OAAO,CAAC,GAAG,C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C;IACtC,CAAC,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACrD,CAAC,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACrD,OAAO,CAAC,CAAC,MAAM,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pluggable storage for persisting the Compose Key JWT across process
|
|
3
|
+
* restarts (browser reload, serverless cold start, etc).
|
|
4
|
+
*
|
|
5
|
+
* The SDK stays framework-agnostic by not importing any browser or Node
|
|
6
|
+
* storage API directly. Integrators pass a minimal adapter; the SDK
|
|
7
|
+
* auto-detects `globalThis.localStorage` when none is provided but runs in a
|
|
8
|
+
* browser, and falls back to an in-memory Map everywhere else.
|
|
9
|
+
*
|
|
10
|
+
* Storage is scoped per `(userAddress, chainId)` tuple so multiple wallets /
|
|
11
|
+
* chains on the same device never collide.
|
|
12
|
+
*/
|
|
13
|
+
export interface ComposeStorage {
|
|
14
|
+
getItem(key: string): string | null;
|
|
15
|
+
setItem(key: string, value: string): void;
|
|
16
|
+
removeItem(key: string): void;
|
|
17
|
+
}
|
|
18
|
+
export declare function resolveStorage(explicit: ComposeStorage | undefined): ComposeStorage | null;
|
|
19
|
+
export declare function createMemoryStorage(): ComposeStorage;
|
|
20
|
+
/**
|
|
21
|
+
* Build the scoped storage key for a persisted Compose Key token. The shape
|
|
22
|
+
* `compose.sdk.token:<lower-address>:<chainId>` mirrors the convention already
|
|
23
|
+
* in use by web/ so multiple SDK instances + the bare web app never collide.
|
|
24
|
+
*/
|
|
25
|
+
export declare function buildTokenStorageKey(tokenScope: string, userAddress: string, chainId: number): string;
|
|
26
|
+
export declare const DEFAULT_TOKEN_SCOPE = "compose.sdk.token";
|
|
27
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,MAAM,WAAW,cAAc;IAC3B,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACpC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1C,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACjC;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,cAAc,GAAG,SAAS,GAAG,cAAc,GAAG,IAAI,CAgB1F;AAED,wBAAgB,mBAAmB,IAAI,cAAc,CAOpD;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAErG;AAED,eAAO,MAAM,mBAAmB,sBAAsB,CAAC"}
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pluggable storage for persisting the Compose Key JWT across process
|
|
3
|
+
* restarts (browser reload, serverless cold start, etc).
|
|
4
|
+
*
|
|
5
|
+
* The SDK stays framework-agnostic by not importing any browser or Node
|
|
6
|
+
* storage API directly. Integrators pass a minimal adapter; the SDK
|
|
7
|
+
* auto-detects `globalThis.localStorage` when none is provided but runs in a
|
|
8
|
+
* browser, and falls back to an in-memory Map everywhere else.
|
|
9
|
+
*
|
|
10
|
+
* Storage is scoped per `(userAddress, chainId)` tuple so multiple wallets /
|
|
11
|
+
* chains on the same device never collide.
|
|
12
|
+
*/
|
|
13
|
+
export function resolveStorage(explicit) {
|
|
14
|
+
if (explicit)
|
|
15
|
+
return explicit;
|
|
16
|
+
// Browser / Deno / Cloudflare Workers with localStorage polyfills.
|
|
17
|
+
const candidate = globalThis.localStorage;
|
|
18
|
+
if (candidate && typeof candidate === "object") {
|
|
19
|
+
const ls = candidate;
|
|
20
|
+
if (typeof ls.getItem === "function" && typeof ls.setItem === "function" && typeof ls.removeItem === "function") {
|
|
21
|
+
return ls;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Node / server runtimes without a persistent store: return null so the
|
|
25
|
+
// SDK falls back to in-memory only. Integrators who want cross-restart
|
|
26
|
+
// persistence in those environments pass an explicit adapter.
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
export function createMemoryStorage() {
|
|
30
|
+
const map = new Map();
|
|
31
|
+
return {
|
|
32
|
+
getItem: (key) => map.get(key) ?? null,
|
|
33
|
+
setItem: (key, value) => { map.set(key, value); },
|
|
34
|
+
removeItem: (key) => { map.delete(key); },
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Build the scoped storage key for a persisted Compose Key token. The shape
|
|
39
|
+
* `compose.sdk.token:<lower-address>:<chainId>` mirrors the convention already
|
|
40
|
+
* in use by web/ so multiple SDK instances + the bare web app never collide.
|
|
41
|
+
*/
|
|
42
|
+
export function buildTokenStorageKey(tokenScope, userAddress, chainId) {
|
|
43
|
+
return `${tokenScope}:${userAddress.toLowerCase()}:${chainId}`;
|
|
44
|
+
}
|
|
45
|
+
export const DEFAULT_TOKEN_SCOPE = "compose.sdk.token";
|
|
46
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAQH,MAAM,UAAU,cAAc,CAAC,QAAoC;IAC/D,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAE9B,mEAAmE;IACnE,MAAM,SAAS,GAAI,UAAyC,CAAC,YAAY,CAAC;IAC1E,IAAI,SAAS,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QAC7C,MAAM,EAAE,GAAG,SAA2B,CAAC;QACvC,IAAI,OAAO,EAAE,CAAC,OAAO,KAAK,UAAU,IAAI,OAAO,EAAE,CAAC,OAAO,KAAK,UAAU,IAAI,OAAO,EAAE,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;YAC9G,OAAO,EAAE,CAAC;QACd,CAAC;IACL,CAAC;IAED,wEAAwE;IACxE,uEAAuE;IACvE,8DAA8D;IAC9D,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,mBAAmB;IAC/B,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtC,OAAO;QACH,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI;QACtC,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QACjD,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;KAC5C,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,UAAkB,EAAE,WAAmB,EAAE,OAAe;IACzF,OAAO,GAAG,UAAU,IAAI,WAAW,CAAC,WAAW,EAAE,IAAI,OAAO,EAAE,CAAC;AACnE,CAAC;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract session-budget state from response headers.
|
|
3
|
+
*
|
|
4
|
+
* The api.compose.market gateway emits the live session-budget ledger on every
|
|
5
|
+
* billable response via these headers:
|
|
6
|
+
* - `x-session-budget-limit` total budget (wei, string)
|
|
7
|
+
* - `x-session-budget-used` spent (wei, string)
|
|
8
|
+
* - `x-session-budget-locked` reserved (wei, string)
|
|
9
|
+
* - `x-session-budget-remaining` remaining (wei, string)
|
|
10
|
+
* - `x-compose-session-invalid` truthy string when the session must be torn
|
|
11
|
+
* down (e.g. "budget-depleted", "expired")
|
|
12
|
+
*
|
|
13
|
+
* This helper converts them into a typed object for the SDK event bus.
|
|
14
|
+
*/
|
|
15
|
+
import type { SessionBudgetSnapshot, SessionInvalidReason } from "../types/index.js";
|
|
16
|
+
export declare function extractSessionBudgetFromResponse(response: {
|
|
17
|
+
headers: Headers;
|
|
18
|
+
}): {
|
|
19
|
+
budget: SessionBudgetSnapshot | null;
|
|
20
|
+
sessionInvalidReason: SessionInvalidReason | null;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.d.ts","sourceRoot":"","sources":["../../src/streaming/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAErF,wBAAgB,gCAAgC,CAC5C,QAAQ,EAAE;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,GAC/B;IAAE,MAAM,EAAE,qBAAqB,GAAG,IAAI,CAAC;IAAC,oBAAoB,EAAE,oBAAoB,GAAG,IAAI,CAAA;CAAE,CA2B7F"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract session-budget state from response headers.
|
|
3
|
+
*
|
|
4
|
+
* The api.compose.market gateway emits the live session-budget ledger on every
|
|
5
|
+
* billable response via these headers:
|
|
6
|
+
* - `x-session-budget-limit` total budget (wei, string)
|
|
7
|
+
* - `x-session-budget-used` spent (wei, string)
|
|
8
|
+
* - `x-session-budget-locked` reserved (wei, string)
|
|
9
|
+
* - `x-session-budget-remaining` remaining (wei, string)
|
|
10
|
+
* - `x-compose-session-invalid` truthy string when the session must be torn
|
|
11
|
+
* down (e.g. "budget-depleted", "expired")
|
|
12
|
+
*
|
|
13
|
+
* This helper converts them into a typed object for the SDK event bus.
|
|
14
|
+
*/
|
|
15
|
+
export function extractSessionBudgetFromResponse(response) {
|
|
16
|
+
const limit = response.headers.get("x-session-budget-limit");
|
|
17
|
+
const used = response.headers.get("x-session-budget-used");
|
|
18
|
+
const locked = response.headers.get("x-session-budget-locked");
|
|
19
|
+
const remaining = response.headers.get("x-session-budget-remaining");
|
|
20
|
+
const invalidRaw = response.headers.get("x-compose-session-invalid");
|
|
21
|
+
const sessionInvalidReason = typeof invalidRaw === "string" && invalidRaw.length > 0
|
|
22
|
+
? invalidRaw
|
|
23
|
+
: null;
|
|
24
|
+
// Emit the budget snapshot only when at least one numeric header is
|
|
25
|
+
// present. Any non-parseable value falls back to null so downstream
|
|
26
|
+
// consumers see `null` rather than NaN.
|
|
27
|
+
if (limit === null && used === null && locked === null && remaining === null) {
|
|
28
|
+
return { budget: null, sessionInvalidReason };
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
budget: {
|
|
32
|
+
limitWei: limit ?? null,
|
|
33
|
+
usedWei: used ?? null,
|
|
34
|
+
lockedWei: locked ?? null,
|
|
35
|
+
remainingWei: remaining ?? null,
|
|
36
|
+
},
|
|
37
|
+
sessionInvalidReason,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.js","sourceRoot":"","sources":["../../src/streaming/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,MAAM,UAAU,gCAAgC,CAC5C,QAA8B;IAE9B,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;IAC7D,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAC3D,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IAC/D,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;IAErE,MAAM,UAAU,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;IACrE,MAAM,oBAAoB,GAAG,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC;QAChF,CAAC,CAAE,UAAmC;QACtC,CAAC,CAAC,IAAI,CAAC;IAEX,oEAAoE;IACpE,oEAAoE;IACpE,wCAAwC;IACxC,IAAI,KAAK,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,IAAI,MAAM,KAAK,IAAI,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QAC3E,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,EAAE,CAAC;IAClD,CAAC;IAED,OAAO;QACH,MAAM,EAAE;YACJ,QAAQ,EAAE,KAAK,IAAI,IAAI;YACvB,OAAO,EAAE,IAAI,IAAI,IAAI;YACrB,SAAS,EAAE,MAAM,IAAI,IAAI;YACzB,YAAY,EAAE,SAAS,IAAI,IAAI;SAClC;QACD,oBAAoB;KACvB,CAAC;AACN,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -133,6 +133,49 @@ export interface ActiveSessionMetadata {
|
|
|
133
133
|
};
|
|
134
134
|
};
|
|
135
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Live session-budget snapshot parsed from `x-session-budget-*` response
|
|
138
|
+
* headers. Every billable response carries these; the SDK surfaces them as
|
|
139
|
+
* part of `ComposeCompletion<T>` and as a typed `budget` event on the event
|
|
140
|
+
* bus so UIs can update the session indicator without polling `/api/session`.
|
|
141
|
+
*/
|
|
142
|
+
export interface SessionBudgetSnapshot {
|
|
143
|
+
limitWei: string | null;
|
|
144
|
+
usedWei: string | null;
|
|
145
|
+
lockedWei: string | null;
|
|
146
|
+
remainingWei: string | null;
|
|
147
|
+
}
|
|
148
|
+
export type SessionInvalidReason = "budget-depleted" | "expired" | "revoked" | "chain-mismatch" | (string & {
|
|
149
|
+
readonly __brand?: "SessionInvalidReason";
|
|
150
|
+
});
|
|
151
|
+
/**
|
|
152
|
+
* SSE-frame payload for `/api/session/events`. The server emits two named
|
|
153
|
+
* events: `session-active` (live ledger heartbeat while the session is
|
|
154
|
+
* healthy) and `session-expired` (terminal notification when the session has
|
|
155
|
+
* been revoked, expired, or run out of budget).
|
|
156
|
+
*/
|
|
157
|
+
export interface SessionActiveEvent {
|
|
158
|
+
type: "session-active";
|
|
159
|
+
userAddress: string;
|
|
160
|
+
chainId: number;
|
|
161
|
+
expiresAt?: number;
|
|
162
|
+
budgetLimit?: string;
|
|
163
|
+
budgetUsed?: string;
|
|
164
|
+
budgetLocked?: string;
|
|
165
|
+
budgetRemaining?: string;
|
|
166
|
+
timestamp?: number;
|
|
167
|
+
}
|
|
168
|
+
export interface SessionExpiredEvent {
|
|
169
|
+
type: "session-expired";
|
|
170
|
+
userAddress: string;
|
|
171
|
+
chainId: number;
|
|
172
|
+
reason?: SessionInvalidReason;
|
|
173
|
+
message?: string;
|
|
174
|
+
action?: string;
|
|
175
|
+
expiresAt?: number;
|
|
176
|
+
timestamp?: number;
|
|
177
|
+
}
|
|
178
|
+
export type SessionEvent = SessionActiveEvent | SessionExpiredEvent;
|
|
136
179
|
export interface ComposeReceiptLineItem {
|
|
137
180
|
key: string;
|
|
138
181
|
unit: string;
|
|
@@ -494,4 +537,126 @@ export type VideoStatusStreamEvent = {
|
|
|
494
537
|
} | {
|
|
495
538
|
type: "done";
|
|
496
539
|
};
|
|
540
|
+
/**
|
|
541
|
+
* Events emitted by the Compose agent runtime on /agent/:wallet/stream.
|
|
542
|
+
* Mirrors the runtime's native SSE vocabulary — no translation, no renaming.
|
|
543
|
+
*/
|
|
544
|
+
export type AgentRuntimeEvent = {
|
|
545
|
+
type: "text-delta";
|
|
546
|
+
delta: string;
|
|
547
|
+
} | {
|
|
548
|
+
type: "thinking-start";
|
|
549
|
+
message: string;
|
|
550
|
+
} | {
|
|
551
|
+
type: "thinking-end";
|
|
552
|
+
} | {
|
|
553
|
+
type: "tool-start";
|
|
554
|
+
toolName: string;
|
|
555
|
+
summary?: string;
|
|
556
|
+
content?: string;
|
|
557
|
+
} | {
|
|
558
|
+
type: "tool-end";
|
|
559
|
+
toolName: string;
|
|
560
|
+
summary?: string;
|
|
561
|
+
failed: boolean;
|
|
562
|
+
error?: string;
|
|
563
|
+
} | {
|
|
564
|
+
type: "error";
|
|
565
|
+
code?: string;
|
|
566
|
+
message: string;
|
|
567
|
+
details?: Record<string, unknown>;
|
|
568
|
+
} | {
|
|
569
|
+
type: "done";
|
|
570
|
+
};
|
|
571
|
+
export interface AgentStreamCreateParams {
|
|
572
|
+
agentWallet: string;
|
|
573
|
+
message: string;
|
|
574
|
+
threadId: string;
|
|
575
|
+
userAddress: string;
|
|
576
|
+
cloudPermissions?: Record<string, unknown>;
|
|
577
|
+
composeRunId?: string;
|
|
578
|
+
attachment?: unknown;
|
|
579
|
+
}
|
|
580
|
+
export interface AgentStreamFinalResult {
|
|
581
|
+
text: string;
|
|
582
|
+
toolCalls: Array<{
|
|
583
|
+
toolName: string;
|
|
584
|
+
summary?: string;
|
|
585
|
+
failed: boolean;
|
|
586
|
+
error?: string;
|
|
587
|
+
}>;
|
|
588
|
+
requestId: string | null;
|
|
589
|
+
receipt: ComposeReceipt | null;
|
|
590
|
+
budget: SessionBudgetSnapshot | null;
|
|
591
|
+
sessionInvalidReason: SessionInvalidReason | null;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Events emitted by the Compose workflow runtime on /workflow/:wallet/chat.
|
|
595
|
+
*/
|
|
596
|
+
export type WorkflowRuntimeEvent = {
|
|
597
|
+
type: "start";
|
|
598
|
+
message: string;
|
|
599
|
+
meta?: Record<string, unknown>;
|
|
600
|
+
} | {
|
|
601
|
+
type: "step";
|
|
602
|
+
stepName?: string;
|
|
603
|
+
message: string;
|
|
604
|
+
meta?: Record<string, unknown>;
|
|
605
|
+
} | {
|
|
606
|
+
type: "agent";
|
|
607
|
+
agentName?: string;
|
|
608
|
+
message: string;
|
|
609
|
+
meta?: Record<string, unknown>;
|
|
610
|
+
} | {
|
|
611
|
+
type: "progress";
|
|
612
|
+
message: string;
|
|
613
|
+
meta?: Record<string, unknown>;
|
|
614
|
+
} | {
|
|
615
|
+
type: "tool-start";
|
|
616
|
+
toolName: string;
|
|
617
|
+
summary?: string;
|
|
618
|
+
content?: string;
|
|
619
|
+
} | {
|
|
620
|
+
type: "tool-end";
|
|
621
|
+
toolName: string;
|
|
622
|
+
summary?: string;
|
|
623
|
+
failed: boolean;
|
|
624
|
+
error?: string;
|
|
625
|
+
} | {
|
|
626
|
+
type: "result";
|
|
627
|
+
output: unknown;
|
|
628
|
+
} | {
|
|
629
|
+
type: "complete";
|
|
630
|
+
message: string;
|
|
631
|
+
} | {
|
|
632
|
+
type: "error";
|
|
633
|
+
code?: string;
|
|
634
|
+
message: string;
|
|
635
|
+
} | {
|
|
636
|
+
type: "done";
|
|
637
|
+
};
|
|
638
|
+
export interface WorkflowStreamCreateParams {
|
|
639
|
+
workflowWallet: string;
|
|
640
|
+
message: string;
|
|
641
|
+
threadId: string;
|
|
642
|
+
userAddress: string;
|
|
643
|
+
composeRunId?: string;
|
|
644
|
+
continuous?: boolean;
|
|
645
|
+
lastEventIndex?: number;
|
|
646
|
+
attachment?: unknown;
|
|
647
|
+
}
|
|
648
|
+
export interface WorkflowStreamFinalResult {
|
|
649
|
+
text: string;
|
|
650
|
+
structuredOutput: unknown;
|
|
651
|
+
toolCalls: Array<{
|
|
652
|
+
toolName: string;
|
|
653
|
+
summary?: string;
|
|
654
|
+
failed: boolean;
|
|
655
|
+
error?: string;
|
|
656
|
+
}>;
|
|
657
|
+
requestId: string | null;
|
|
658
|
+
receipt: ComposeReceipt | null;
|
|
659
|
+
budget: SessionBudgetSnapshot | null;
|
|
660
|
+
sessionInvalidReason: SessionInvalidReason | null;
|
|
661
|
+
}
|
|
497
662
|
//# sourceMappingURL=index.d.ts.map
|