@yourgpt/llm-sdk 1.5.2 → 1.5.4
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/dist/index.d.mts +167 -13
- package/dist/index.d.ts +167 -13
- package/dist/index.js +189 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +189 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1907,6 +1907,84 @@ function createStreamResult(generator) {
|
|
|
1907
1907
|
return new StreamResult(generator);
|
|
1908
1908
|
}
|
|
1909
1909
|
|
|
1910
|
+
// src/server/generate-result.ts
|
|
1911
|
+
var GenerateResult = class {
|
|
1912
|
+
constructor(data) {
|
|
1913
|
+
this.data = data;
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Generated text content
|
|
1917
|
+
*/
|
|
1918
|
+
get text() {
|
|
1919
|
+
return this.data.text;
|
|
1920
|
+
}
|
|
1921
|
+
/**
|
|
1922
|
+
* All messages from the conversation
|
|
1923
|
+
*/
|
|
1924
|
+
get messages() {
|
|
1925
|
+
return this.data.messages;
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Tool calls made during generation
|
|
1929
|
+
*/
|
|
1930
|
+
get toolCalls() {
|
|
1931
|
+
return this.data.toolCalls;
|
|
1932
|
+
}
|
|
1933
|
+
/**
|
|
1934
|
+
* Results from tool executions
|
|
1935
|
+
*/
|
|
1936
|
+
get toolResults() {
|
|
1937
|
+
return this.data.toolResults;
|
|
1938
|
+
}
|
|
1939
|
+
/**
|
|
1940
|
+
* Whether client action is required (e.g., tool approval)
|
|
1941
|
+
*/
|
|
1942
|
+
get requiresAction() {
|
|
1943
|
+
return this.data.requiresAction;
|
|
1944
|
+
}
|
|
1945
|
+
/**
|
|
1946
|
+
* Error if generation failed
|
|
1947
|
+
*/
|
|
1948
|
+
get error() {
|
|
1949
|
+
return this.data.error;
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* Whether generation was successful
|
|
1953
|
+
*/
|
|
1954
|
+
get success() {
|
|
1955
|
+
return !this.data.error;
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Convert to CopilotChat-compatible JSON response
|
|
1959
|
+
*
|
|
1960
|
+
* @example
|
|
1961
|
+
* ```typescript
|
|
1962
|
+
* // Express
|
|
1963
|
+
* res.json(result.toResponse());
|
|
1964
|
+
*
|
|
1965
|
+
* // Next.js
|
|
1966
|
+
* return Response.json(result.toResponse());
|
|
1967
|
+
* ```
|
|
1968
|
+
*/
|
|
1969
|
+
toResponse() {
|
|
1970
|
+
return {
|
|
1971
|
+
success: this.success,
|
|
1972
|
+
content: this.data.text,
|
|
1973
|
+
messages: this.data.messages.length > 0 ? this.data.messages : void 0,
|
|
1974
|
+
toolCalls: this.data.toolCalls.length > 0 ? this.data.toolCalls : void 0,
|
|
1975
|
+
toolResults: this.data.toolResults.length > 0 ? this.data.toolResults : void 0,
|
|
1976
|
+
requiresAction: this.data.requiresAction || void 0,
|
|
1977
|
+
error: this.data.error
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* Convert to raw object (without methods)
|
|
1982
|
+
*/
|
|
1983
|
+
toJSON() {
|
|
1984
|
+
return { ...this.data };
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
|
|
1910
1988
|
// src/server/runtime.ts
|
|
1911
1989
|
function buildToolResultForAI(tool2, result, args) {
|
|
1912
1990
|
const typedResult = result;
|
|
@@ -3069,14 +3147,119 @@ var Runtime = class {
|
|
|
3069
3147
|
* console.log('Response:', text);
|
|
3070
3148
|
* res.json({ response: text });
|
|
3071
3149
|
*
|
|
3072
|
-
* //
|
|
3073
|
-
* const
|
|
3074
|
-
*
|
|
3075
|
-
*
|
|
3150
|
+
* // Usage is included in result - strip before sending to client
|
|
3151
|
+
* const { usage, ...clientResult } = await runtime.chat(body);
|
|
3152
|
+
* await billing.record(usage);
|
|
3153
|
+
* res.json(clientResult);
|
|
3076
3154
|
* ```
|
|
3077
3155
|
*/
|
|
3078
3156
|
async chat(request, options) {
|
|
3079
|
-
return this.stream(request, options).collect(
|
|
3157
|
+
return this.stream(request, { signal: options?.signal }).collect({
|
|
3158
|
+
includeUsage: true
|
|
3159
|
+
});
|
|
3160
|
+
}
|
|
3161
|
+
/**
|
|
3162
|
+
* Generate a complete response (non-streaming)
|
|
3163
|
+
*
|
|
3164
|
+
* Like Vercel AI SDK's generateText() - clean, non-streaming API.
|
|
3165
|
+
* Returns GenerateResult with .toResponse() for CopilotChat format.
|
|
3166
|
+
*
|
|
3167
|
+
* @example
|
|
3168
|
+
* ```typescript
|
|
3169
|
+
* // Simple usage
|
|
3170
|
+
* const result = await runtime.generate(body);
|
|
3171
|
+
* console.log(result.text);
|
|
3172
|
+
*
|
|
3173
|
+
* // CopilotChat format response (Express)
|
|
3174
|
+
* res.json(result.toResponse());
|
|
3175
|
+
*
|
|
3176
|
+
* // CopilotChat format response (Next.js)
|
|
3177
|
+
* return Response.json(result.toResponse());
|
|
3178
|
+
*
|
|
3179
|
+
* // With persistence callback
|
|
3180
|
+
* const result = await runtime.generate(body, {
|
|
3181
|
+
* onFinish: async ({ messages }) => {
|
|
3182
|
+
* await db.saveMessages(messages);
|
|
3183
|
+
* },
|
|
3184
|
+
* });
|
|
3185
|
+
* ```
|
|
3186
|
+
*/
|
|
3187
|
+
async generate(request, options) {
|
|
3188
|
+
const generator = this.processChatWithLoop(
|
|
3189
|
+
{ ...request, streaming: false },
|
|
3190
|
+
options?.signal,
|
|
3191
|
+
void 0,
|
|
3192
|
+
void 0,
|
|
3193
|
+
options?.httpRequest
|
|
3194
|
+
);
|
|
3195
|
+
let text = "";
|
|
3196
|
+
const toolCalls = [];
|
|
3197
|
+
const toolResults = [];
|
|
3198
|
+
let messages = [];
|
|
3199
|
+
let requiresAction = false;
|
|
3200
|
+
let error;
|
|
3201
|
+
try {
|
|
3202
|
+
for await (const event of generator) {
|
|
3203
|
+
switch (event.type) {
|
|
3204
|
+
case "message:delta":
|
|
3205
|
+
text += event.content;
|
|
3206
|
+
break;
|
|
3207
|
+
case "action:start":
|
|
3208
|
+
toolCalls.push({ id: event.id, name: event.name, args: {} });
|
|
3209
|
+
break;
|
|
3210
|
+
case "action:args": {
|
|
3211
|
+
const tc = toolCalls.find((t) => t.id === event.id);
|
|
3212
|
+
if (tc) {
|
|
3213
|
+
try {
|
|
3214
|
+
tc.args = JSON.parse(event.args || "{}");
|
|
3215
|
+
} catch {
|
|
3216
|
+
tc.args = {};
|
|
3217
|
+
}
|
|
3218
|
+
}
|
|
3219
|
+
break;
|
|
3220
|
+
}
|
|
3221
|
+
case "action:end":
|
|
3222
|
+
toolResults.push({
|
|
3223
|
+
id: event.id,
|
|
3224
|
+
result: event.result || event.error
|
|
3225
|
+
});
|
|
3226
|
+
break;
|
|
3227
|
+
case "done":
|
|
3228
|
+
messages = event.messages || [];
|
|
3229
|
+
requiresAction = event.requiresAction || false;
|
|
3230
|
+
break;
|
|
3231
|
+
case "error":
|
|
3232
|
+
error = { message: event.message, code: event.code };
|
|
3233
|
+
break;
|
|
3234
|
+
}
|
|
3235
|
+
}
|
|
3236
|
+
} catch (err) {
|
|
3237
|
+
error = {
|
|
3238
|
+
message: err instanceof Error ? err.message : "Unknown error",
|
|
3239
|
+
code: "GENERATION_ERROR"
|
|
3240
|
+
};
|
|
3241
|
+
}
|
|
3242
|
+
if (options?.onFinish && messages.length > 0 && !error) {
|
|
3243
|
+
try {
|
|
3244
|
+
await options.onFinish({
|
|
3245
|
+
messages,
|
|
3246
|
+
threadId: request.threadId
|
|
3247
|
+
});
|
|
3248
|
+
} catch (callbackError) {
|
|
3249
|
+
console.error(
|
|
3250
|
+
"[Copilot SDK] generate() onFinish callback error:",
|
|
3251
|
+
callbackError
|
|
3252
|
+
);
|
|
3253
|
+
}
|
|
3254
|
+
}
|
|
3255
|
+
return new GenerateResult({
|
|
3256
|
+
text,
|
|
3257
|
+
messages,
|
|
3258
|
+
toolCalls,
|
|
3259
|
+
toolResults,
|
|
3260
|
+
requiresAction,
|
|
3261
|
+
error
|
|
3262
|
+
});
|
|
3080
3263
|
}
|
|
3081
3264
|
/**
|
|
3082
3265
|
* Create Express-compatible handler middleware
|
|
@@ -3781,6 +3964,6 @@ async function executeToolCalls(toolCalls, tools, executeServerTool, waitForClie
|
|
|
3781
3964
|
return results;
|
|
3782
3965
|
}
|
|
3783
3966
|
|
|
3784
|
-
export { DEFAULT_CAPABILITIES, DEFAULT_MAX_ITERATIONS, Runtime, StreamResult, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, streamText, tool };
|
|
3967
|
+
export { DEFAULT_CAPABILITIES, DEFAULT_MAX_ITERATIONS, GenerateResult, Runtime, StreamResult, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, streamText, tool };
|
|
3785
3968
|
//# sourceMappingURL=index.mjs.map
|
|
3786
3969
|
//# sourceMappingURL=index.mjs.map
|