catelog 1.0.1 → 1.0.3
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/DECISION.md +183 -183
- package/README.md +98 -74
- package/USAGE.md +958 -0
- package/dist/adapters.d.ts +114 -0
- package/dist/adapters.d.ts.map +1 -0
- package/dist/adapters.js +347 -0
- package/dist/adapters.js.map +1 -0
- package/dist/agent.d.ts +73 -13
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +373 -154
- package/dist/agent.js.map +1 -1
- package/dist/banner.d.ts +6 -3
- package/dist/banner.d.ts.map +1 -1
- package/dist/banner.js +29 -18
- package/dist/banner.js.map +1 -1
- package/dist/context.d.ts +18 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +82 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -6
- package/dist/index.js.map +1 -1
- package/dist/security.d.ts +25 -16
- package/dist/security.d.ts.map +1 -1
- package/dist/security.js +132 -55
- package/dist/security.js.map +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +8 -3
- package/dist/server.js.map +1 -1
- package/dist/sse.d.ts +31 -7
- package/dist/sse.d.ts.map +1 -1
- package/dist/sse.js +94 -26
- package/dist/sse.js.map +1 -1
- package/dist/telemetry.d.ts +41 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +93 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/types.d.ts +115 -27
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework Adapters
|
|
3
|
+
*
|
|
4
|
+
* Translates framework-specific streaming patterns into the Catelog
|
|
5
|
+
* task lifecycle contract. Each adapter is a thin transformer that:
|
|
6
|
+
*
|
|
7
|
+
* 1. Pipes framework chunks → stream.delta() / stream.progress()
|
|
8
|
+
* 2. Extracts telemetry (tokens, tool calls) when available
|
|
9
|
+
* 3. Returns the final result in Catelog's { output, telemetry } format
|
|
10
|
+
*
|
|
11
|
+
* All adapters are framework-optional — if the framework library
|
|
12
|
+
* isn't installed, the adapter gracefully skips enrichment.
|
|
13
|
+
*/
|
|
14
|
+
import type { AgentExecutionResult, AgentStream } from "./types.js";
|
|
15
|
+
/**
|
|
16
|
+
* Pipes a Vercel AI SDK streamText/generateText result into Catelog events.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { streamText } from "ai";
|
|
21
|
+
* import { openai } from "@ai-sdk/openai";
|
|
22
|
+
*
|
|
23
|
+
* async execute({ input, stream }) {
|
|
24
|
+
* const result = streamText({
|
|
25
|
+
* model: openai("gpt-4o"),
|
|
26
|
+
* prompt: input.prompt,
|
|
27
|
+
* });
|
|
28
|
+
* return pipeAiSdkStream(result, stream);
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function pipeAiSdkStream(aiResult: any, stream: AgentStream): Promise<AgentExecutionResult>;
|
|
33
|
+
/**
|
|
34
|
+
* Pipes an OpenAI SDK chat.completions.create({ stream: true }) result.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import OpenAI from "openai";
|
|
39
|
+
* const client = new OpenAI();
|
|
40
|
+
*
|
|
41
|
+
* async execute({ input, stream }) {
|
|
42
|
+
* const openaiStream = await client.chat.completions.create({
|
|
43
|
+
* model: "gpt-4o-mini",
|
|
44
|
+
* messages: [{ role: "user", content: input.prompt }],
|
|
45
|
+
* stream: true,
|
|
46
|
+
* });
|
|
47
|
+
* return pipeOpenAiStream(openaiStream, stream);
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function pipeOpenAiStream(openaiStream: any, stream: AgentStream): Promise<AgentExecutionResult>;
|
|
52
|
+
/**
|
|
53
|
+
* Pipes a LangGraph compiled graph's stream into Catelog events.
|
|
54
|
+
* Translates graph node transitions to progress events and token
|
|
55
|
+
* deltas to stream chunks.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* async execute({ input, stream }) {
|
|
60
|
+
* return pipeLangGraphStream(compiledGraph, { input }, stream);
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function pipeLangGraphStream(graph: any, graphInput: Record<string, unknown>, stream: AgentStream, options?: {
|
|
65
|
+
signal?: AbortSignal;
|
|
66
|
+
}): Promise<AgentExecutionResult>;
|
|
67
|
+
/**
|
|
68
|
+
* For any framework that produces an async iterable of text chunks
|
|
69
|
+
* (CrewAI, LlamaIndex, custom generators, etc.)
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* async execute({ input, stream }) {
|
|
74
|
+
* const chunks = myCustomAgentStream(input);
|
|
75
|
+
* return pipeAsyncIterableStream(chunks, stream);
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function pipeAsyncIterableStream(iterable: AsyncIterable<string | {
|
|
80
|
+
text?: string;
|
|
81
|
+
content?: string;
|
|
82
|
+
delta?: string;
|
|
83
|
+
}>, stream: AgentStream): Promise<AgentExecutionResult>;
|
|
84
|
+
/**
|
|
85
|
+
* Pipes an OpenAI Responses API result into Catelog streaming events.
|
|
86
|
+
* Supports:
|
|
87
|
+
* - Non-streaming: pass the resolved response object directly
|
|
88
|
+
* - Background polling: pass the polling loop as an async iterable via pollFn
|
|
89
|
+
*
|
|
90
|
+
* Emits:
|
|
91
|
+
* - stream.delta() for each text chunk / output_text
|
|
92
|
+
* - stream.activity("web_search") for search tool calls
|
|
93
|
+
* - stream.activity("reasoning") for reasoning steps
|
|
94
|
+
* - Real token telemetry from response.usage
|
|
95
|
+
*
|
|
96
|
+
* @example — Fast (non-streaming):
|
|
97
|
+
* ```ts
|
|
98
|
+
* const response = await client.responses.create({ model: "gpt-6-luna", input, tools: [{ type: "web_search" }] });
|
|
99
|
+
* return pipeOpenAiResponsesStream(response, stream);
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example — Deep/background with polling:
|
|
103
|
+
* ```ts
|
|
104
|
+
* let res = await client.responses.create({ model: "gpt-5.4-mini", input, background: true });
|
|
105
|
+
* while (res.status === "queued" || res.status === "in_progress") {
|
|
106
|
+
* stream.activity("polling", { status: res.status, taskId: res.id });
|
|
107
|
+
* await sleep(2000);
|
|
108
|
+
* res = await client.responses.retrieve(res.id);
|
|
109
|
+
* }
|
|
110
|
+
* return pipeOpenAiResponsesStream(res, stream);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare function pipeOpenAiResponsesStream(response: any, stream: AgentStream): Promise<AgentExecutionResult>;
|
|
114
|
+
//# sourceMappingURL=adapters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapters.d.ts","sourceRoot":"","sources":["../src/adapters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EACV,oBAAoB,EACpB,WAAW,EAEZ,MAAM,YAAY,CAAC;AAMpB;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,eAAe,CACnC,QAAQ,EAAE,GAAG,EACb,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,oBAAoB,CAAC,CAwD/B;AAMD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,gBAAgB,CACpC,YAAY,EAAE,GAAG,EACjB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,oBAAoB,CAAC,CAkE/B;AAMD;;;;;;;;;;;GAWG;AACH,wBAAsB,mBAAmB,CACvC,KAAK,EAAE,GAAG,EACV,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,MAAM,EAAE,WAAW,EACnB,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,GACjC,OAAO,CAAC,oBAAoB,CAAC,CAwD/B;AAMD;;;;;;;;;;;GAWG;AACH,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,aAAa,CAAC,MAAM,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,EACrF,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,oBAAoB,CAAC,CAgB/B;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,yBAAyB,CAC7C,QAAQ,EAAE,GAAG,EACb,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,oBAAoB,CAAC,CAkD/B"}
|
package/dist/adapters.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework Adapters
|
|
3
|
+
*
|
|
4
|
+
* Translates framework-specific streaming patterns into the Catelog
|
|
5
|
+
* task lifecycle contract. Each adapter is a thin transformer that:
|
|
6
|
+
*
|
|
7
|
+
* 1. Pipes framework chunks → stream.delta() / stream.progress()
|
|
8
|
+
* 2. Extracts telemetry (tokens, tool calls) when available
|
|
9
|
+
* 3. Returns the final result in Catelog's { output, telemetry } format
|
|
10
|
+
*
|
|
11
|
+
* All adapters are framework-optional — if the framework library
|
|
12
|
+
* isn't installed, the adapter gracefully skips enrichment.
|
|
13
|
+
*/
|
|
14
|
+
// ─────────────────────────────────────────────────
|
|
15
|
+
// Vercel AI SDK Adapter
|
|
16
|
+
// ─────────────────────────────────────────────────
|
|
17
|
+
/**
|
|
18
|
+
* Pipes a Vercel AI SDK streamText/generateText result into Catelog events.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { streamText } from "ai";
|
|
23
|
+
* import { openai } from "@ai-sdk/openai";
|
|
24
|
+
*
|
|
25
|
+
* async execute({ input, stream }) {
|
|
26
|
+
* const result = streamText({
|
|
27
|
+
* model: openai("gpt-4o"),
|
|
28
|
+
* prompt: input.prompt,
|
|
29
|
+
* });
|
|
30
|
+
* return pipeAiSdkStream(result, stream);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export async function pipeAiSdkStream(aiResult, stream) {
|
|
35
|
+
let fullText = "";
|
|
36
|
+
const telemetry = { framework: { adapter: "vercel-ai-sdk" } };
|
|
37
|
+
try {
|
|
38
|
+
// streamText returns a StreamTextResult with textStream
|
|
39
|
+
if (aiResult?.textStream && Symbol.asyncIterator in aiResult.textStream) {
|
|
40
|
+
for await (const chunk of aiResult.textStream) {
|
|
41
|
+
if (typeof chunk === "string" && chunk.length > 0) {
|
|
42
|
+
stream.delta(chunk);
|
|
43
|
+
fullText += chunk;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// generateText returns a GenerateTextResult with .text
|
|
48
|
+
else if (typeof aiResult?.text === "string") {
|
|
49
|
+
fullText = aiResult.text;
|
|
50
|
+
stream.delta(fullText);
|
|
51
|
+
}
|
|
52
|
+
// Fallback: try to iterate directly
|
|
53
|
+
else if (aiResult && Symbol.asyncIterator in aiResult) {
|
|
54
|
+
for await (const chunk of aiResult) {
|
|
55
|
+
const text = typeof chunk === "string" ? chunk : chunk?.textDelta || chunk?.delta || "";
|
|
56
|
+
if (text) {
|
|
57
|
+
stream.delta(text);
|
|
58
|
+
fullText += text;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Extract token usage if available
|
|
63
|
+
if (aiResult?.usage) {
|
|
64
|
+
telemetry.llm_tokens = {
|
|
65
|
+
prompt: aiResult.usage.promptTokens ?? aiResult.usage.prompt_tokens,
|
|
66
|
+
completion: aiResult.usage.completionTokens ?? aiResult.usage.completion_tokens,
|
|
67
|
+
total: aiResult.usage.totalTokens ?? aiResult.usage.total_tokens,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Extract tool calls if present
|
|
71
|
+
if (aiResult?.toolCalls && Array.isArray(aiResult.toolCalls)) {
|
|
72
|
+
telemetry.framework = {
|
|
73
|
+
...telemetry.framework,
|
|
74
|
+
tool_calls: aiResult.toolCalls.length,
|
|
75
|
+
tools_used: aiResult.toolCalls.map((tc) => tc.toolName || tc.name),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
// Graceful fallback — adapter failures don't crash the agent
|
|
81
|
+
if (fullText) {
|
|
82
|
+
return { output: { text: fullText }, telemetry };
|
|
83
|
+
}
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
return { output: { text: fullText }, telemetry };
|
|
87
|
+
}
|
|
88
|
+
// ─────────────────────────────────────────────────
|
|
89
|
+
// OpenAI SDK Stream Adapter
|
|
90
|
+
// ─────────────────────────────────────────────────
|
|
91
|
+
/**
|
|
92
|
+
* Pipes an OpenAI SDK chat.completions.create({ stream: true }) result.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import OpenAI from "openai";
|
|
97
|
+
* const client = new OpenAI();
|
|
98
|
+
*
|
|
99
|
+
* async execute({ input, stream }) {
|
|
100
|
+
* const openaiStream = await client.chat.completions.create({
|
|
101
|
+
* model: "gpt-4o-mini",
|
|
102
|
+
* messages: [{ role: "user", content: input.prompt }],
|
|
103
|
+
* stream: true,
|
|
104
|
+
* });
|
|
105
|
+
* return pipeOpenAiStream(openaiStream, stream);
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export async function pipeOpenAiStream(openaiStream, stream) {
|
|
110
|
+
let fullText = "";
|
|
111
|
+
let model;
|
|
112
|
+
let promptTokens;
|
|
113
|
+
let completionTokens;
|
|
114
|
+
const telemetry = { framework: { adapter: "openai" } };
|
|
115
|
+
try {
|
|
116
|
+
if (openaiStream && Symbol.asyncIterator in openaiStream) {
|
|
117
|
+
for await (const chunk of openaiStream) {
|
|
118
|
+
// Standard OpenAI chat completion chunk
|
|
119
|
+
const delta = chunk?.choices?.[0]?.delta;
|
|
120
|
+
if (delta?.content) {
|
|
121
|
+
stream.delta(delta.content);
|
|
122
|
+
fullText += delta.content;
|
|
123
|
+
}
|
|
124
|
+
// Tool calls in streaming mode
|
|
125
|
+
if (delta?.tool_calls) {
|
|
126
|
+
for (const tc of delta.tool_calls) {
|
|
127
|
+
stream.activity("tool_call", {
|
|
128
|
+
name: tc.function?.name,
|
|
129
|
+
arguments: tc.function?.arguments,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// Capture model name
|
|
134
|
+
if (chunk?.model && !model) {
|
|
135
|
+
model = chunk.model;
|
|
136
|
+
}
|
|
137
|
+
// Usage in final chunk (OpenAI includes this with stream_options: { include_usage: true })
|
|
138
|
+
if (chunk?.usage) {
|
|
139
|
+
promptTokens = chunk.usage.prompt_tokens;
|
|
140
|
+
completionTokens = chunk.usage.completion_tokens;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// Non-streaming response
|
|
145
|
+
else if (openaiStream?.choices?.[0]?.message?.content) {
|
|
146
|
+
fullText = openaiStream.choices[0].message.content;
|
|
147
|
+
stream.delta(fullText);
|
|
148
|
+
model = openaiStream.model;
|
|
149
|
+
if (openaiStream.usage) {
|
|
150
|
+
promptTokens = openaiStream.usage.prompt_tokens;
|
|
151
|
+
completionTokens = openaiStream.usage.completion_tokens;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (model)
|
|
155
|
+
telemetry.model_used = model;
|
|
156
|
+
if (promptTokens !== undefined || completionTokens !== undefined) {
|
|
157
|
+
telemetry.llm_tokens = {
|
|
158
|
+
prompt: promptTokens,
|
|
159
|
+
completion: completionTokens,
|
|
160
|
+
total: (promptTokens ?? 0) + (completionTokens ?? 0),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
if (fullText) {
|
|
166
|
+
return { output: { text: fullText }, telemetry };
|
|
167
|
+
}
|
|
168
|
+
throw err;
|
|
169
|
+
}
|
|
170
|
+
return { output: { text: fullText }, telemetry };
|
|
171
|
+
}
|
|
172
|
+
// ─────────────────────────────────────────────────
|
|
173
|
+
// LangGraph / LangChain Adapter
|
|
174
|
+
// ─────────────────────────────────────────────────
|
|
175
|
+
/**
|
|
176
|
+
* Pipes a LangGraph compiled graph's stream into Catelog events.
|
|
177
|
+
* Translates graph node transitions to progress events and token
|
|
178
|
+
* deltas to stream chunks.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* async execute({ input, stream }) {
|
|
183
|
+
* return pipeLangGraphStream(compiledGraph, { input }, stream);
|
|
184
|
+
* }
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export async function pipeLangGraphStream(graph, graphInput, stream, options) {
|
|
188
|
+
let lastOutput = undefined;
|
|
189
|
+
let nodeCount = 0;
|
|
190
|
+
const nodesVisited = [];
|
|
191
|
+
const telemetry = { framework: { adapter: "langgraph" } };
|
|
192
|
+
try {
|
|
193
|
+
// LangGraph .stream() returns an async iterable of state snapshots
|
|
194
|
+
const streamMethod = graph.stream || graph.streamEvents;
|
|
195
|
+
if (!streamMethod) {
|
|
196
|
+
// Fallback: invoke synchronously
|
|
197
|
+
const result = await graph.invoke(graphInput, { signal: options?.signal });
|
|
198
|
+
return { output: result, telemetry };
|
|
199
|
+
}
|
|
200
|
+
const graphStream = await streamMethod.call(graph, graphInput, {
|
|
201
|
+
signal: options?.signal,
|
|
202
|
+
streamMode: "updates",
|
|
203
|
+
});
|
|
204
|
+
for await (const event of graphStream) {
|
|
205
|
+
// Each event is typically { nodeName: nodeOutput }
|
|
206
|
+
if (typeof event === "object" && event !== null) {
|
|
207
|
+
for (const [nodeName, nodeOutput] of Object.entries(event)) {
|
|
208
|
+
nodeCount++;
|
|
209
|
+
nodesVisited.push(nodeName);
|
|
210
|
+
stream.activity("graph_node", { node: nodeName, step: nodeCount });
|
|
211
|
+
// If node output has messages with content, stream them
|
|
212
|
+
if (nodeOutput && typeof nodeOutput === "object") {
|
|
213
|
+
const messages = nodeOutput.messages || [];
|
|
214
|
+
for (const msg of messages) {
|
|
215
|
+
if (msg?.content && typeof msg.content === "string") {
|
|
216
|
+
stream.delta(msg.content);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
lastOutput = nodeOutput;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
telemetry.framework = {
|
|
225
|
+
...telemetry.framework,
|
|
226
|
+
nodes_visited: nodesVisited,
|
|
227
|
+
node_count: nodeCount,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
if (lastOutput) {
|
|
232
|
+
return { output: lastOutput, telemetry };
|
|
233
|
+
}
|
|
234
|
+
throw err;
|
|
235
|
+
}
|
|
236
|
+
return { output: (lastOutput ?? {}), telemetry };
|
|
237
|
+
}
|
|
238
|
+
// ─────────────────────────────────────────────────
|
|
239
|
+
// Generic Async Iterable Adapter
|
|
240
|
+
// ─────────────────────────────────────────────────
|
|
241
|
+
/**
|
|
242
|
+
* For any framework that produces an async iterable of text chunks
|
|
243
|
+
* (CrewAI, LlamaIndex, custom generators, etc.)
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```ts
|
|
247
|
+
* async execute({ input, stream }) {
|
|
248
|
+
* const chunks = myCustomAgentStream(input);
|
|
249
|
+
* return pipeAsyncIterableStream(chunks, stream);
|
|
250
|
+
* }
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
export async function pipeAsyncIterableStream(iterable, stream) {
|
|
254
|
+
let fullText = "";
|
|
255
|
+
const telemetry = { framework: { adapter: "generic-iterable" } };
|
|
256
|
+
for await (const chunk of iterable) {
|
|
257
|
+
const text = typeof chunk === "string"
|
|
258
|
+
? chunk
|
|
259
|
+
: (chunk?.text || chunk?.content || chunk?.delta || "");
|
|
260
|
+
if (text) {
|
|
261
|
+
stream.delta(text);
|
|
262
|
+
fullText += text;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return { output: { text: fullText }, telemetry };
|
|
266
|
+
}
|
|
267
|
+
// ─────────────────────────────────────────────────
|
|
268
|
+
// OpenAI Responses API Adapter (client.responses.create)
|
|
269
|
+
// ─────────────────────────────────────────────────
|
|
270
|
+
/**
|
|
271
|
+
* Pipes an OpenAI Responses API result into Catelog streaming events.
|
|
272
|
+
* Supports:
|
|
273
|
+
* - Non-streaming: pass the resolved response object directly
|
|
274
|
+
* - Background polling: pass the polling loop as an async iterable via pollFn
|
|
275
|
+
*
|
|
276
|
+
* Emits:
|
|
277
|
+
* - stream.delta() for each text chunk / output_text
|
|
278
|
+
* - stream.activity("web_search") for search tool calls
|
|
279
|
+
* - stream.activity("reasoning") for reasoning steps
|
|
280
|
+
* - Real token telemetry from response.usage
|
|
281
|
+
*
|
|
282
|
+
* @example — Fast (non-streaming):
|
|
283
|
+
* ```ts
|
|
284
|
+
* const response = await client.responses.create({ model: "gpt-6-luna", input, tools: [{ type: "web_search" }] });
|
|
285
|
+
* return pipeOpenAiResponsesStream(response, stream);
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* @example — Deep/background with polling:
|
|
289
|
+
* ```ts
|
|
290
|
+
* let res = await client.responses.create({ model: "gpt-5.4-mini", input, background: true });
|
|
291
|
+
* while (res.status === "queued" || res.status === "in_progress") {
|
|
292
|
+
* stream.activity("polling", { status: res.status, taskId: res.id });
|
|
293
|
+
* await sleep(2000);
|
|
294
|
+
* res = await client.responses.retrieve(res.id);
|
|
295
|
+
* }
|
|
296
|
+
* return pipeOpenAiResponsesStream(res, stream);
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
export async function pipeOpenAiResponsesStream(response, stream) {
|
|
300
|
+
const telemetry = { framework: { adapter: "openai-responses" } };
|
|
301
|
+
// ── Emit tool/activity events from output items ──
|
|
302
|
+
const outputItems = response?.output ?? [];
|
|
303
|
+
for (const item of outputItems) {
|
|
304
|
+
if (item?.type === "web_search_call" || item?.type === "search") {
|
|
305
|
+
stream.activity("web_search", {
|
|
306
|
+
query: item?.action?.query ?? item?.query ?? undefined,
|
|
307
|
+
status: item?.status ?? "completed",
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (item?.type === "reasoning") {
|
|
311
|
+
const reasoningText = item?.summary?.map((s) => s?.text).join(" ") ?? "";
|
|
312
|
+
if (reasoningText) {
|
|
313
|
+
stream.activity("reasoning", { summary: reasoningText.slice(0, 200) });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// ── Extract the final answer text ──
|
|
318
|
+
const fullText = response?.output_text ?? "";
|
|
319
|
+
if (fullText) {
|
|
320
|
+
stream.delta(fullText);
|
|
321
|
+
}
|
|
322
|
+
// ── Real token telemetry ──
|
|
323
|
+
if (response?.usage) {
|
|
324
|
+
telemetry.llm_tokens = {
|
|
325
|
+
prompt: response.usage.input_tokens ?? response.usage.prompt_tokens,
|
|
326
|
+
completion: response.usage.output_tokens ?? response.usage.completion_tokens,
|
|
327
|
+
total: response.usage.total_tokens ??
|
|
328
|
+
((response.usage.input_tokens ?? 0) + (response.usage.output_tokens ?? 0)),
|
|
329
|
+
};
|
|
330
|
+
// Cost estimation (adjust per model pricing)
|
|
331
|
+
const promptTokens = telemetry.llm_tokens.prompt ?? 0;
|
|
332
|
+
const completionTokens = telemetry.llm_tokens.completion ?? 0;
|
|
333
|
+
const modelName = response.model ?? "";
|
|
334
|
+
if (modelName.includes("gpt-5") || modelName.includes("mini")) {
|
|
335
|
+
telemetry.cost_usd = parseFloat(((promptTokens * 0.00000015) + (completionTokens * 0.0000006)).toFixed(6));
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
telemetry.cost_usd = parseFloat(((promptTokens * 0.000005) + (completionTokens * 0.000015)).toFixed(6));
|
|
339
|
+
}
|
|
340
|
+
telemetry.model_used = modelName;
|
|
341
|
+
}
|
|
342
|
+
return {
|
|
343
|
+
output: { text: fullText },
|
|
344
|
+
telemetry,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
//# sourceMappingURL=adapters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapters.js","sourceRoot":"","sources":["../src/adapters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAQH,oDAAoD;AACpD,yBAAyB;AACzB,oDAAoD;AAEpD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,QAAa,EACb,MAAmB;IAEnB,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,MAAM,SAAS,GAAkB,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,eAAe,EAAE,EAAE,CAAC;IAE7E,IAAI,CAAC;QACH,wDAAwD;QACxD,IAAI,QAAQ,EAAE,UAAU,IAAI,MAAM,CAAC,aAAa,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;YACxE,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;gBAC9C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAClD,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBACpB,QAAQ,IAAI,KAAK,CAAC;gBACpB,CAAC;YACH,CAAC;QACH,CAAC;QACD,uDAAuD;aAClD,IAAI,OAAO,QAAQ,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC5C,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC;YACzB,MAAM,CA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AAI,CAAC,CAAC,CAAC;SAC7E,CAAC;QACF,6CAA6C;QAC7C,MAAM,YAAY,GAAG,SAAS,CAAC,UAAU,CAAC,MAAM,IAAI,CAAC,CAAC;QACtD,MAAM,gBAAgB,GAAG,SAAS,CAAC,UAAU,CAAC,UAAU,IAAI,CAAC,CAAC;QAC9D,MAAM,SAAS,GAAW,QAAQ,CAAC,KAAK,IAAI,EAAE,CAAC;QAC/C,IAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YAC9D,SAAS,CAAC,QAAQ,GAAG,UAAU,CAAC,CAAC,CAAC,YAAY,GAAG,UAAU,CAAC,GAAG,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7G,CAAC;aAAM,CAAC;YACN,SAAS,CAAC,QAAQ,GAAG,UAAU,CAAC,CAAC,CAAC,YAAY,GAAG,QAAQ,CAAC,GAAG,CAAC,gBAAgB,GAAG,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1G,CAAC;QACD,SAAS,CAAC,UAAU,GAAG,SAAS,CAAC;IACnC,CAAC;IAED,OAAO;QACL,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAS;QACjC,SAAS;KACV,CAAC;AACJ,CAAC"}
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,38 +1,98 @@
|
|
|
1
1
|
import { SecurityManager } from "./security.js";
|
|
2
2
|
import { type SchemaLike } from "./schema.js";
|
|
3
|
-
import { type AgentCapabilities, type AgentExecutionContext, type AgentExecutionResult, type AgentManifest, type HealthCheckResponse } from "./types.js";
|
|
3
|
+
import { type AgentCapabilities, type AgentExecutionContext, type AgentExecutionResult, type AgentManifest, type ExecutionMode, type HealthCheckResponse, type NeedInputResult } from "./types.js";
|
|
4
4
|
import { type ServerOptions } from "./server.js";
|
|
5
5
|
import type { IncomingMessage, ServerResponse, Server } from "node:http";
|
|
6
|
-
export interface
|
|
6
|
+
export interface CatelogAgentConfig<TInput = any, TOutput = any> {
|
|
7
|
+
/** Unique slug ID (auto-generated from name if omitted) */
|
|
7
8
|
id?: string;
|
|
9
|
+
/** Human-readable agent name */
|
|
8
10
|
name: string;
|
|
11
|
+
/** Semantic version (default: "1.0.0") */
|
|
9
12
|
version?: string;
|
|
13
|
+
/** Short description for marketplace listing */
|
|
10
14
|
description?: string;
|
|
11
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Secret key for HMAC / Bearer auth.
|
|
17
|
+
* Auto-reads CATELOG_SECRET_KEY from env if omitted.
|
|
18
|
+
*/
|
|
19
|
+
secretKey?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Publishable key for public endpoints.
|
|
22
|
+
* Auto-reads CATELOG_PUBLIC_KEY from env if omitted.
|
|
23
|
+
*/
|
|
24
|
+
publicKey?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Execution mode:
|
|
27
|
+
* - "sync" (default): Direct HTTP/SSE stream, resolves in same connection.
|
|
28
|
+
* - "async": Returns 202 immediately, polled via GET /tasks/:id.
|
|
29
|
+
* - "conversational": Supports ctx.needInput() for HITL multi-turn.
|
|
30
|
+
*/
|
|
31
|
+
mode?: ExecutionMode;
|
|
32
|
+
/** Partial capabilities overrides */
|
|
12
33
|
capabilities?: Partial<AgentCapabilities>;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Input schema (Zod or JSON Schema). OPTIONAL — if omitted, accepts any JSON.
|
|
36
|
+
*/
|
|
37
|
+
inputSchema?: SchemaLike;
|
|
38
|
+
/**
|
|
39
|
+
* Output schema (Zod or JSON Schema). OPTIONAL — if omitted, no output validation.
|
|
40
|
+
*/
|
|
41
|
+
outputSchema?: SchemaLike;
|
|
42
|
+
/**
|
|
43
|
+
* The core execution function. This is the ONLY thing the developer MUST provide.
|
|
44
|
+
*
|
|
45
|
+
* - Return a value → task completes automatically
|
|
46
|
+
* - Throw an error → task fails automatically
|
|
47
|
+
* - Use ctx.needInput() → task suspends for human input
|
|
48
|
+
* - Use stream.delta() / stream.progress() → real-time events to marketplace
|
|
49
|
+
*/
|
|
50
|
+
execute: (ctx: AgentExecutionContext<TInput>) => Promise<AgentExecutionResult<TOutput> | NeedInputResult | TOutput>;
|
|
51
|
+
/** Optional telemetry reporting config */
|
|
52
|
+
telemetry?: {
|
|
53
|
+
endpoint?: string;
|
|
54
|
+
disabled?: boolean;
|
|
55
|
+
};
|
|
16
56
|
}
|
|
17
|
-
export declare class
|
|
57
|
+
export declare class CatelogAgent<TInput = any, TOutput = any> {
|
|
18
58
|
readonly id: string;
|
|
19
59
|
readonly name: string;
|
|
20
60
|
readonly version: string;
|
|
21
61
|
readonly description: string;
|
|
62
|
+
readonly mode: ExecutionMode;
|
|
22
63
|
readonly capabilities: AgentCapabilities;
|
|
23
64
|
readonly security: SecurityManager;
|
|
24
65
|
private readonly config;
|
|
25
66
|
private readonly startTime;
|
|
26
|
-
|
|
67
|
+
private readonly telemetryReporter;
|
|
68
|
+
constructor(config: CatelogAgentConfig<TInput, TOutput>);
|
|
27
69
|
getHealth(): HealthCheckResponse;
|
|
28
70
|
getManifest(): AgentManifest;
|
|
71
|
+
listen(portOrOptions?: number | ServerOptions): Server;
|
|
72
|
+
/** Alias for backward compat */
|
|
29
73
|
serve(portOrOptions?: number | ServerOptions): Server;
|
|
30
74
|
handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
31
75
|
private handleExecute;
|
|
32
|
-
private
|
|
76
|
+
private executeAsync;
|
|
77
|
+
private executeSyncOrConversational;
|
|
78
|
+
private executeWithSSE;
|
|
79
|
+
private executeWithJSON;
|
|
80
|
+
private handleTaskPoll;
|
|
81
|
+
/**
|
|
82
|
+
* Normalize the developer's return value into our standard result shape.
|
|
83
|
+
* Supports:
|
|
84
|
+
* - { output: T, telemetry?: ... } → standard result
|
|
85
|
+
* - Any other value → treated as the output directly
|
|
86
|
+
*/
|
|
87
|
+
private normalizeExecuteResult;
|
|
88
|
+
private isNeedInputResult;
|
|
89
|
+
private reportTelemetry;
|
|
90
|
+
private writeJson;
|
|
33
91
|
}
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
*/
|
|
37
|
-
export declare function createMarketplaceAgent<TInput =
|
|
92
|
+
/** Create a new CatelogAgent */
|
|
93
|
+
export declare function createAgent<TInput = any, TOutput = any>(config: CatelogAgentConfig<TInput, TOutput>): CatelogAgent<TInput, TOutput>;
|
|
94
|
+
/** Legacy alias */
|
|
95
|
+
export declare function createMarketplaceAgent<TInput = any, TOutput = any>(config: CatelogAgentConfig<TInput, TOutput>): CatelogAgent<TInput, TOutput>;
|
|
96
|
+
export { CatelogAgent as MarketplaceAgent };
|
|
97
|
+
export type { CatelogAgentConfig as MarketplaceAgentConfig };
|
|
38
98
|
//# sourceMappingURL=agent.d.ts.map
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAA6B,MAAM,eAAe,CAAC;AAE3E,OAAO,EAAoC,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAShF,OAAO,EAGL,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAGlB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EAGrB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAoB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAMzE,MAAM,WAAW,kBAAkB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