@inkeep/ai-sdk-provider 0.0.0-dev-20251222193816 → 0.0.0-dev-20251222214618
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/convert-to-inkeep-messages.cjs +82 -0
- package/dist/convert-to-inkeep-messages.d.cts +7 -0
- package/dist/convert-to-inkeep-messages.d.ts +7 -0
- package/dist/convert-to-inkeep-messages.js +81 -0
- package/dist/get-response-metadata.cjs +12 -0
- package/dist/get-response-metadata.d.cts +10 -0
- package/dist/get-response-metadata.d.ts +10 -0
- package/dist/get-response-metadata.js +11 -0
- package/dist/index.cjs +5 -3511
- package/dist/index.d.cts +6 -197
- package/dist/index.d.ts +6 -198
- package/dist/index.js +3 -3510
- package/dist/inkeep-chat-language-model.cjs +270 -0
- package/dist/inkeep-chat-language-model.d.cts +81 -0
- package/dist/inkeep-chat-language-model.d.ts +81 -0
- package/dist/inkeep-chat-language-model.js +269 -0
- package/dist/inkeep-chat-options.cjs +0 -0
- package/dist/inkeep-chat-options.d.cts +8 -0
- package/dist/inkeep-chat-options.d.ts +8 -0
- package/dist/inkeep-chat-options.js +1 -0
- package/dist/inkeep-chat-prompt.cjs +0 -0
- package/dist/inkeep-chat-prompt.d.cts +84 -0
- package/dist/inkeep-chat-prompt.d.ts +84 -0
- package/dist/inkeep-chat-prompt.js +1 -0
- package/dist/inkeep-error.cjs +20 -0
- package/dist/inkeep-error.d.cts +18 -0
- package/dist/inkeep-error.d.ts +18 -0
- package/dist/inkeep-error.js +20 -0
- package/dist/inkeep-provider.cjs +35 -0
- package/dist/inkeep-provider.d.cts +18 -0
- package/dist/inkeep-provider.d.ts +18 -0
- package/dist/inkeep-provider.js +34 -0
- package/dist/map-inkeep-finish-reason.cjs +15 -0
- package/dist/map-inkeep-finish-reason.d.cts +7 -0
- package/dist/map-inkeep-finish-reason.d.ts +7 -0
- package/dist/map-inkeep-finish-reason.js +14 -0
- package/package.json +4 -3
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
const require_convert_to_inkeep_messages = require('./convert-to-inkeep-messages.cjs');
|
|
2
|
+
const require_get_response_metadata = require('./get-response-metadata.cjs');
|
|
3
|
+
const require_inkeep_error = require('./inkeep-error.cjs');
|
|
4
|
+
const require_map_inkeep_finish_reason = require('./map-inkeep-finish-reason.cjs');
|
|
5
|
+
let __ai_sdk_provider_utils = require("@ai-sdk/provider-utils");
|
|
6
|
+
let __inkeep_agents_core = require("@inkeep/agents-core");
|
|
7
|
+
let zod = require("zod");
|
|
8
|
+
|
|
9
|
+
//#region src/inkeep-chat-language-model.ts
|
|
10
|
+
/**
|
|
11
|
+
* Converts data-operation events to AI SDK tool events (tool-call or tool-result).
|
|
12
|
+
*
|
|
13
|
+
* Handles:
|
|
14
|
+
* - tool_call: Direct tool invocation (tool-call)
|
|
15
|
+
* - tool_result: Direct tool result (tool-result)
|
|
16
|
+
* - transfer: Agent transfers control to another agent (tool-call)
|
|
17
|
+
* - delegation_sent: Agent delegates a task to another agent (tool-call)
|
|
18
|
+
* - delegation_returned: Delegated task completes and returns (tool-result)
|
|
19
|
+
*/
|
|
20
|
+
function convertDataOperationToToolEvent(opData) {
|
|
21
|
+
if (!opData || typeof opData !== "object" || !("type" in opData)) return null;
|
|
22
|
+
const eventType = opData.type;
|
|
23
|
+
const data = opData.details?.data;
|
|
24
|
+
if (!data) return null;
|
|
25
|
+
switch (eventType) {
|
|
26
|
+
case "tool_call": return {
|
|
27
|
+
type: "tool-call",
|
|
28
|
+
toolCallId: data.toolCallId,
|
|
29
|
+
toolName: data.toolName,
|
|
30
|
+
input: JSON.stringify(data.input)
|
|
31
|
+
};
|
|
32
|
+
case "tool_result": return {
|
|
33
|
+
type: "tool-result",
|
|
34
|
+
toolCallId: data.toolCallId,
|
|
35
|
+
toolName: data.toolName,
|
|
36
|
+
result: data.output?.result ?? data.output
|
|
37
|
+
};
|
|
38
|
+
case "transfer": {
|
|
39
|
+
const transferData = data;
|
|
40
|
+
return {
|
|
41
|
+
type: "tool-call",
|
|
42
|
+
toolCallId: `transfer_${Date.now()}`,
|
|
43
|
+
toolName: transferData.targetSubAgent,
|
|
44
|
+
input: JSON.stringify({
|
|
45
|
+
fromSubAgent: transferData.fromSubAgent,
|
|
46
|
+
reason: transferData.reason,
|
|
47
|
+
context: transferData.context
|
|
48
|
+
})
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
case "delegation_sent": {
|
|
52
|
+
const delegationData = data;
|
|
53
|
+
return {
|
|
54
|
+
type: "tool-call",
|
|
55
|
+
toolCallId: delegationData.delegationId,
|
|
56
|
+
toolName: delegationData.targetSubAgent,
|
|
57
|
+
input: JSON.stringify({
|
|
58
|
+
fromSubAgent: delegationData.fromSubAgent,
|
|
59
|
+
taskDescription: delegationData.taskDescription,
|
|
60
|
+
context: delegationData.context
|
|
61
|
+
})
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
case "delegation_returned": {
|
|
65
|
+
const returnedData = data;
|
|
66
|
+
return {
|
|
67
|
+
type: "tool-result",
|
|
68
|
+
toolCallId: returnedData.delegationId,
|
|
69
|
+
toolName: returnedData.targetSubAgent,
|
|
70
|
+
result: returnedData.result
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
default: return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
var InkeepChatLanguageModel = class {
|
|
77
|
+
specificationVersion = "v2";
|
|
78
|
+
defaultObjectGenerationMode = void 0;
|
|
79
|
+
supportsImageUrls = false;
|
|
80
|
+
supportedUrls = {};
|
|
81
|
+
modelId;
|
|
82
|
+
options;
|
|
83
|
+
config;
|
|
84
|
+
get provider() {
|
|
85
|
+
return this.config.provider;
|
|
86
|
+
}
|
|
87
|
+
constructor(options, config) {
|
|
88
|
+
this.modelId = "inkeep-agent";
|
|
89
|
+
this.options = options;
|
|
90
|
+
this.config = config;
|
|
91
|
+
}
|
|
92
|
+
getArgs(options) {
|
|
93
|
+
return {
|
|
94
|
+
args: {
|
|
95
|
+
messages: require_convert_to_inkeep_messages.convertToInkeepChatMessages(options.prompt),
|
|
96
|
+
conversationId: this.options.conversationId,
|
|
97
|
+
headers: this.options.headers,
|
|
98
|
+
runConfig: this.options.runConfig
|
|
99
|
+
},
|
|
100
|
+
warnings: []
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async doGenerate(options) {
|
|
104
|
+
const { args, warnings } = this.getArgs(options);
|
|
105
|
+
const response = await (0, __ai_sdk_provider_utils.postJsonToApi)({
|
|
106
|
+
url: `${this.config.baseURL}/api/chat`,
|
|
107
|
+
headers: this.config.headers(),
|
|
108
|
+
body: {
|
|
109
|
+
...args,
|
|
110
|
+
stream: false
|
|
111
|
+
},
|
|
112
|
+
failedResponseHandler: require_inkeep_error.inkeepFailedResponseHandler,
|
|
113
|
+
successfulResponseHandler: (0, __ai_sdk_provider_utils.createJsonResponseHandler)(inkeepChatCompletionSchema),
|
|
114
|
+
abortSignal: options.abortSignal,
|
|
115
|
+
fetch: this.config.fetch
|
|
116
|
+
});
|
|
117
|
+
const { value: completion } = response;
|
|
118
|
+
const choice = completion.choices[0];
|
|
119
|
+
const content = [];
|
|
120
|
+
if (choice.message.content) content.push({
|
|
121
|
+
type: "text",
|
|
122
|
+
text: choice.message.content
|
|
123
|
+
});
|
|
124
|
+
return {
|
|
125
|
+
content,
|
|
126
|
+
finishReason: require_map_inkeep_finish_reason.mapInkeepFinishReason(choice.finish_reason),
|
|
127
|
+
usage: {
|
|
128
|
+
promptTokens: completion.usage?.prompt_tokens ?? 0,
|
|
129
|
+
completionTokens: completion.usage?.completion_tokens ?? 0,
|
|
130
|
+
inputTokens: completion.usage?.prompt_tokens ?? 0,
|
|
131
|
+
outputTokens: completion.usage?.completion_tokens ?? 0,
|
|
132
|
+
totalTokens: completion.usage?.total_tokens ?? 0
|
|
133
|
+
},
|
|
134
|
+
rawCall: {
|
|
135
|
+
rawPrompt: args.messages,
|
|
136
|
+
rawSettings: args
|
|
137
|
+
},
|
|
138
|
+
rawResponse: { headers: response.responseHeaders },
|
|
139
|
+
response: require_get_response_metadata.getResponseMetadata(completion),
|
|
140
|
+
warnings
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async doStream(options) {
|
|
144
|
+
const { args, warnings } = this.getArgs(options);
|
|
145
|
+
const response = await (0, __ai_sdk_provider_utils.postJsonToApi)({
|
|
146
|
+
url: `${this.config.baseURL}/api/chat`,
|
|
147
|
+
headers: this.config.headers(),
|
|
148
|
+
body: {
|
|
149
|
+
...args,
|
|
150
|
+
stream: true
|
|
151
|
+
},
|
|
152
|
+
failedResponseHandler: require_inkeep_error.inkeepFailedResponseHandler,
|
|
153
|
+
successfulResponseHandler: (0, __ai_sdk_provider_utils.createEventSourceResponseHandler)(__inkeep_agents_core.StreamEventSchema),
|
|
154
|
+
abortSignal: options.abortSignal,
|
|
155
|
+
fetch: this.config.fetch
|
|
156
|
+
});
|
|
157
|
+
const { value: stream } = response;
|
|
158
|
+
let finishReason = "unknown";
|
|
159
|
+
let usage = {
|
|
160
|
+
promptTokens: 0,
|
|
161
|
+
completionTokens: 0,
|
|
162
|
+
inputTokens: 0,
|
|
163
|
+
outputTokens: 0,
|
|
164
|
+
totalTokens: 0
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
stream: stream.pipeThrough(new TransformStream({
|
|
168
|
+
start(controller) {
|
|
169
|
+
controller.enqueue({
|
|
170
|
+
type: "stream-start",
|
|
171
|
+
warnings
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
transform(chunk, controller) {
|
|
175
|
+
if (!chunk.success) {
|
|
176
|
+
controller.enqueue({
|
|
177
|
+
type: "error",
|
|
178
|
+
error: chunk.error
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const event = chunk.value;
|
|
183
|
+
switch (event.type) {
|
|
184
|
+
case "text-start":
|
|
185
|
+
controller.enqueue({
|
|
186
|
+
type: "text-start",
|
|
187
|
+
id: event.id
|
|
188
|
+
});
|
|
189
|
+
break;
|
|
190
|
+
case "text-delta":
|
|
191
|
+
controller.enqueue({
|
|
192
|
+
type: "text-delta",
|
|
193
|
+
id: event.id,
|
|
194
|
+
delta: event.delta
|
|
195
|
+
});
|
|
196
|
+
break;
|
|
197
|
+
case "text-end":
|
|
198
|
+
controller.enqueue({
|
|
199
|
+
type: "text-end",
|
|
200
|
+
id: event.id
|
|
201
|
+
});
|
|
202
|
+
break;
|
|
203
|
+
case "data-component":
|
|
204
|
+
case "data-summary": break;
|
|
205
|
+
case "data-operation": {
|
|
206
|
+
const toolEvent = convertDataOperationToToolEvent(event.data);
|
|
207
|
+
if (toolEvent) controller.enqueue(toolEvent);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
case "error":
|
|
211
|
+
controller.enqueue({
|
|
212
|
+
type: "error",
|
|
213
|
+
error: new Error(event.error)
|
|
214
|
+
});
|
|
215
|
+
break;
|
|
216
|
+
case "finish":
|
|
217
|
+
if (event.finishReason) finishReason = require_map_inkeep_finish_reason.mapInkeepFinishReason(event.finishReason);
|
|
218
|
+
if (event.usage) usage = {
|
|
219
|
+
promptTokens: event.usage.promptTokens ?? 0,
|
|
220
|
+
completionTokens: event.usage.completionTokens ?? 0,
|
|
221
|
+
inputTokens: event.usage.promptTokens ?? 0,
|
|
222
|
+
outputTokens: event.usage.completionTokens ?? 0,
|
|
223
|
+
totalTokens: event.usage.totalTokens ?? 0
|
|
224
|
+
};
|
|
225
|
+
break;
|
|
226
|
+
default:
|
|
227
|
+
console.warn("Unhandled stream event type");
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
flush(controller) {
|
|
232
|
+
controller.enqueue({
|
|
233
|
+
type: "finish",
|
|
234
|
+
finishReason,
|
|
235
|
+
usage
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
})),
|
|
239
|
+
rawCall: {
|
|
240
|
+
rawPrompt: args.messages,
|
|
241
|
+
rawSettings: args
|
|
242
|
+
},
|
|
243
|
+
rawResponse: { headers: response.responseHeaders },
|
|
244
|
+
warnings
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const inkeepChatCompletionSchema = zod.z.object({
|
|
249
|
+
id: zod.z.string(),
|
|
250
|
+
object: zod.z.literal("chat.completion"),
|
|
251
|
+
created: zod.z.number(),
|
|
252
|
+
model: zod.z.string().optional(),
|
|
253
|
+
choices: zod.z.array(zod.z.object({
|
|
254
|
+
index: zod.z.number(),
|
|
255
|
+
message: zod.z.object({
|
|
256
|
+
role: zod.z.literal("assistant"),
|
|
257
|
+
content: zod.z.string()
|
|
258
|
+
}),
|
|
259
|
+
finish_reason: zod.z.string()
|
|
260
|
+
})),
|
|
261
|
+
usage: zod.z.object({
|
|
262
|
+
prompt_tokens: zod.z.number().optional(),
|
|
263
|
+
completion_tokens: zod.z.number().optional(),
|
|
264
|
+
total_tokens: zod.z.number().optional()
|
|
265
|
+
}).optional()
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
exports.InkeepChatLanguageModel = InkeepChatLanguageModel;
|
|
270
|
+
exports.convertDataOperationToToolEvent = convertDataOperationToToolEvent;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { InkeepChatMessage } from "./inkeep-chat-prompt.cjs";
|
|
2
|
+
import { InkeepChatOptions } from "./inkeep-chat-options.cjs";
|
|
3
|
+
import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2CallWarning, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2StreamPart } from "@ai-sdk/provider";
|
|
4
|
+
|
|
5
|
+
//#region src/inkeep-chat-language-model.d.ts
|
|
6
|
+
interface InkeepChatConfig {
|
|
7
|
+
provider: string;
|
|
8
|
+
baseURL: string;
|
|
9
|
+
headers: () => Record<string, string | undefined>;
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Converts data-operation events to AI SDK tool events (tool-call or tool-result).
|
|
14
|
+
*
|
|
15
|
+
* Handles:
|
|
16
|
+
* - tool_call: Direct tool invocation (tool-call)
|
|
17
|
+
* - tool_result: Direct tool result (tool-result)
|
|
18
|
+
* - transfer: Agent transfers control to another agent (tool-call)
|
|
19
|
+
* - delegation_sent: Agent delegates a task to another agent (tool-call)
|
|
20
|
+
* - delegation_returned: Delegated task completes and returns (tool-result)
|
|
21
|
+
*/
|
|
22
|
+
declare function convertDataOperationToToolEvent(opData: any): LanguageModelV2StreamPart | null;
|
|
23
|
+
declare class InkeepChatLanguageModel implements LanguageModelV2 {
|
|
24
|
+
readonly specificationVersion: "v2";
|
|
25
|
+
readonly defaultObjectGenerationMode: undefined;
|
|
26
|
+
readonly supportsImageUrls = false;
|
|
27
|
+
readonly supportedUrls: {};
|
|
28
|
+
readonly modelId: string;
|
|
29
|
+
readonly options: InkeepChatOptions;
|
|
30
|
+
readonly config: InkeepChatConfig;
|
|
31
|
+
get provider(): string;
|
|
32
|
+
constructor(options: InkeepChatOptions, config: InkeepChatConfig);
|
|
33
|
+
private getArgs;
|
|
34
|
+
doGenerate(options: LanguageModelV2CallOptions): Promise<{
|
|
35
|
+
content: LanguageModelV2Content[];
|
|
36
|
+
finishReason: LanguageModelV2FinishReason;
|
|
37
|
+
usage: {
|
|
38
|
+
promptTokens: number;
|
|
39
|
+
completionTokens: number;
|
|
40
|
+
inputTokens: number;
|
|
41
|
+
outputTokens: number;
|
|
42
|
+
totalTokens: number;
|
|
43
|
+
};
|
|
44
|
+
rawCall: {
|
|
45
|
+
rawPrompt: InkeepChatMessage[];
|
|
46
|
+
rawSettings: {
|
|
47
|
+
messages: InkeepChatMessage[];
|
|
48
|
+
conversationId: string | undefined;
|
|
49
|
+
headers: Record<string, unknown> | undefined;
|
|
50
|
+
runConfig: Record<string, unknown> | undefined;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
rawResponse: {
|
|
54
|
+
headers: Record<string, string> | undefined;
|
|
55
|
+
};
|
|
56
|
+
response: {
|
|
57
|
+
id: string;
|
|
58
|
+
modelId: string | undefined;
|
|
59
|
+
timestamp: Date;
|
|
60
|
+
};
|
|
61
|
+
warnings: LanguageModelV2CallWarning[];
|
|
62
|
+
}>;
|
|
63
|
+
doStream(options: LanguageModelV2CallOptions): Promise<{
|
|
64
|
+
stream: ReadableStream<LanguageModelV2StreamPart>;
|
|
65
|
+
rawCall: {
|
|
66
|
+
rawPrompt: InkeepChatMessage[];
|
|
67
|
+
rawSettings: {
|
|
68
|
+
messages: InkeepChatMessage[];
|
|
69
|
+
conversationId: string | undefined;
|
|
70
|
+
headers: Record<string, unknown> | undefined;
|
|
71
|
+
runConfig: Record<string, unknown> | undefined;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
rawResponse: {
|
|
75
|
+
headers: Record<string, string> | undefined;
|
|
76
|
+
};
|
|
77
|
+
warnings: LanguageModelV2CallWarning[];
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { InkeepChatLanguageModel, convertDataOperationToToolEvent };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { InkeepChatMessage } from "./inkeep-chat-prompt.js";
|
|
2
|
+
import { InkeepChatOptions } from "./inkeep-chat-options.js";
|
|
3
|
+
import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2CallWarning, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2StreamPart } from "@ai-sdk/provider";
|
|
4
|
+
|
|
5
|
+
//#region src/inkeep-chat-language-model.d.ts
|
|
6
|
+
interface InkeepChatConfig {
|
|
7
|
+
provider: string;
|
|
8
|
+
baseURL: string;
|
|
9
|
+
headers: () => Record<string, string | undefined>;
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Converts data-operation events to AI SDK tool events (tool-call or tool-result).
|
|
14
|
+
*
|
|
15
|
+
* Handles:
|
|
16
|
+
* - tool_call: Direct tool invocation (tool-call)
|
|
17
|
+
* - tool_result: Direct tool result (tool-result)
|
|
18
|
+
* - transfer: Agent transfers control to another agent (tool-call)
|
|
19
|
+
* - delegation_sent: Agent delegates a task to another agent (tool-call)
|
|
20
|
+
* - delegation_returned: Delegated task completes and returns (tool-result)
|
|
21
|
+
*/
|
|
22
|
+
declare function convertDataOperationToToolEvent(opData: any): LanguageModelV2StreamPart | null;
|
|
23
|
+
declare class InkeepChatLanguageModel implements LanguageModelV2 {
|
|
24
|
+
readonly specificationVersion: "v2";
|
|
25
|
+
readonly defaultObjectGenerationMode: undefined;
|
|
26
|
+
readonly supportsImageUrls = false;
|
|
27
|
+
readonly supportedUrls: {};
|
|
28
|
+
readonly modelId: string;
|
|
29
|
+
readonly options: InkeepChatOptions;
|
|
30
|
+
readonly config: InkeepChatConfig;
|
|
31
|
+
get provider(): string;
|
|
32
|
+
constructor(options: InkeepChatOptions, config: InkeepChatConfig);
|
|
33
|
+
private getArgs;
|
|
34
|
+
doGenerate(options: LanguageModelV2CallOptions): Promise<{
|
|
35
|
+
content: LanguageModelV2Content[];
|
|
36
|
+
finishReason: LanguageModelV2FinishReason;
|
|
37
|
+
usage: {
|
|
38
|
+
promptTokens: number;
|
|
39
|
+
completionTokens: number;
|
|
40
|
+
inputTokens: number;
|
|
41
|
+
outputTokens: number;
|
|
42
|
+
totalTokens: number;
|
|
43
|
+
};
|
|
44
|
+
rawCall: {
|
|
45
|
+
rawPrompt: InkeepChatMessage[];
|
|
46
|
+
rawSettings: {
|
|
47
|
+
messages: InkeepChatMessage[];
|
|
48
|
+
conversationId: string | undefined;
|
|
49
|
+
headers: Record<string, unknown> | undefined;
|
|
50
|
+
runConfig: Record<string, unknown> | undefined;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
rawResponse: {
|
|
54
|
+
headers: Record<string, string> | undefined;
|
|
55
|
+
};
|
|
56
|
+
response: {
|
|
57
|
+
id: string;
|
|
58
|
+
modelId: string | undefined;
|
|
59
|
+
timestamp: Date;
|
|
60
|
+
};
|
|
61
|
+
warnings: LanguageModelV2CallWarning[];
|
|
62
|
+
}>;
|
|
63
|
+
doStream(options: LanguageModelV2CallOptions): Promise<{
|
|
64
|
+
stream: ReadableStream<LanguageModelV2StreamPart>;
|
|
65
|
+
rawCall: {
|
|
66
|
+
rawPrompt: InkeepChatMessage[];
|
|
67
|
+
rawSettings: {
|
|
68
|
+
messages: InkeepChatMessage[];
|
|
69
|
+
conversationId: string | undefined;
|
|
70
|
+
headers: Record<string, unknown> | undefined;
|
|
71
|
+
runConfig: Record<string, unknown> | undefined;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
rawResponse: {
|
|
75
|
+
headers: Record<string, string> | undefined;
|
|
76
|
+
};
|
|
77
|
+
warnings: LanguageModelV2CallWarning[];
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { InkeepChatLanguageModel, convertDataOperationToToolEvent };
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { convertToInkeepChatMessages } from "./convert-to-inkeep-messages.js";
|
|
2
|
+
import { getResponseMetadata } from "./get-response-metadata.js";
|
|
3
|
+
import { inkeepFailedResponseHandler } from "./inkeep-error.js";
|
|
4
|
+
import { mapInkeepFinishReason } from "./map-inkeep-finish-reason.js";
|
|
5
|
+
import { createEventSourceResponseHandler, createJsonResponseHandler, postJsonToApi } from "@ai-sdk/provider-utils";
|
|
6
|
+
import { StreamEventSchema } from "@inkeep/agents-core";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
|
|
9
|
+
//#region src/inkeep-chat-language-model.ts
|
|
10
|
+
/**
|
|
11
|
+
* Converts data-operation events to AI SDK tool events (tool-call or tool-result).
|
|
12
|
+
*
|
|
13
|
+
* Handles:
|
|
14
|
+
* - tool_call: Direct tool invocation (tool-call)
|
|
15
|
+
* - tool_result: Direct tool result (tool-result)
|
|
16
|
+
* - transfer: Agent transfers control to another agent (tool-call)
|
|
17
|
+
* - delegation_sent: Agent delegates a task to another agent (tool-call)
|
|
18
|
+
* - delegation_returned: Delegated task completes and returns (tool-result)
|
|
19
|
+
*/
|
|
20
|
+
function convertDataOperationToToolEvent(opData) {
|
|
21
|
+
if (!opData || typeof opData !== "object" || !("type" in opData)) return null;
|
|
22
|
+
const eventType = opData.type;
|
|
23
|
+
const data = opData.details?.data;
|
|
24
|
+
if (!data) return null;
|
|
25
|
+
switch (eventType) {
|
|
26
|
+
case "tool_call": return {
|
|
27
|
+
type: "tool-call",
|
|
28
|
+
toolCallId: data.toolCallId,
|
|
29
|
+
toolName: data.toolName,
|
|
30
|
+
input: JSON.stringify(data.input)
|
|
31
|
+
};
|
|
32
|
+
case "tool_result": return {
|
|
33
|
+
type: "tool-result",
|
|
34
|
+
toolCallId: data.toolCallId,
|
|
35
|
+
toolName: data.toolName,
|
|
36
|
+
result: data.output?.result ?? data.output
|
|
37
|
+
};
|
|
38
|
+
case "transfer": {
|
|
39
|
+
const transferData = data;
|
|
40
|
+
return {
|
|
41
|
+
type: "tool-call",
|
|
42
|
+
toolCallId: `transfer_${Date.now()}`,
|
|
43
|
+
toolName: transferData.targetSubAgent,
|
|
44
|
+
input: JSON.stringify({
|
|
45
|
+
fromSubAgent: transferData.fromSubAgent,
|
|
46
|
+
reason: transferData.reason,
|
|
47
|
+
context: transferData.context
|
|
48
|
+
})
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
case "delegation_sent": {
|
|
52
|
+
const delegationData = data;
|
|
53
|
+
return {
|
|
54
|
+
type: "tool-call",
|
|
55
|
+
toolCallId: delegationData.delegationId,
|
|
56
|
+
toolName: delegationData.targetSubAgent,
|
|
57
|
+
input: JSON.stringify({
|
|
58
|
+
fromSubAgent: delegationData.fromSubAgent,
|
|
59
|
+
taskDescription: delegationData.taskDescription,
|
|
60
|
+
context: delegationData.context
|
|
61
|
+
})
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
case "delegation_returned": {
|
|
65
|
+
const returnedData = data;
|
|
66
|
+
return {
|
|
67
|
+
type: "tool-result",
|
|
68
|
+
toolCallId: returnedData.delegationId,
|
|
69
|
+
toolName: returnedData.targetSubAgent,
|
|
70
|
+
result: returnedData.result
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
default: return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
var InkeepChatLanguageModel = class {
|
|
77
|
+
specificationVersion = "v2";
|
|
78
|
+
defaultObjectGenerationMode = void 0;
|
|
79
|
+
supportsImageUrls = false;
|
|
80
|
+
supportedUrls = {};
|
|
81
|
+
modelId;
|
|
82
|
+
options;
|
|
83
|
+
config;
|
|
84
|
+
get provider() {
|
|
85
|
+
return this.config.provider;
|
|
86
|
+
}
|
|
87
|
+
constructor(options, config) {
|
|
88
|
+
this.modelId = "inkeep-agent";
|
|
89
|
+
this.options = options;
|
|
90
|
+
this.config = config;
|
|
91
|
+
}
|
|
92
|
+
getArgs(options) {
|
|
93
|
+
return {
|
|
94
|
+
args: {
|
|
95
|
+
messages: convertToInkeepChatMessages(options.prompt),
|
|
96
|
+
conversationId: this.options.conversationId,
|
|
97
|
+
headers: this.options.headers,
|
|
98
|
+
runConfig: this.options.runConfig
|
|
99
|
+
},
|
|
100
|
+
warnings: []
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async doGenerate(options) {
|
|
104
|
+
const { args, warnings } = this.getArgs(options);
|
|
105
|
+
const response = await postJsonToApi({
|
|
106
|
+
url: `${this.config.baseURL}/api/chat`,
|
|
107
|
+
headers: this.config.headers(),
|
|
108
|
+
body: {
|
|
109
|
+
...args,
|
|
110
|
+
stream: false
|
|
111
|
+
},
|
|
112
|
+
failedResponseHandler: inkeepFailedResponseHandler,
|
|
113
|
+
successfulResponseHandler: createJsonResponseHandler(inkeepChatCompletionSchema),
|
|
114
|
+
abortSignal: options.abortSignal,
|
|
115
|
+
fetch: this.config.fetch
|
|
116
|
+
});
|
|
117
|
+
const { value: completion } = response;
|
|
118
|
+
const choice = completion.choices[0];
|
|
119
|
+
const content = [];
|
|
120
|
+
if (choice.message.content) content.push({
|
|
121
|
+
type: "text",
|
|
122
|
+
text: choice.message.content
|
|
123
|
+
});
|
|
124
|
+
return {
|
|
125
|
+
content,
|
|
126
|
+
finishReason: mapInkeepFinishReason(choice.finish_reason),
|
|
127
|
+
usage: {
|
|
128
|
+
promptTokens: completion.usage?.prompt_tokens ?? 0,
|
|
129
|
+
completionTokens: completion.usage?.completion_tokens ?? 0,
|
|
130
|
+
inputTokens: completion.usage?.prompt_tokens ?? 0,
|
|
131
|
+
outputTokens: completion.usage?.completion_tokens ?? 0,
|
|
132
|
+
totalTokens: completion.usage?.total_tokens ?? 0
|
|
133
|
+
},
|
|
134
|
+
rawCall: {
|
|
135
|
+
rawPrompt: args.messages,
|
|
136
|
+
rawSettings: args
|
|
137
|
+
},
|
|
138
|
+
rawResponse: { headers: response.responseHeaders },
|
|
139
|
+
response: getResponseMetadata(completion),
|
|
140
|
+
warnings
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async doStream(options) {
|
|
144
|
+
const { args, warnings } = this.getArgs(options);
|
|
145
|
+
const response = await postJsonToApi({
|
|
146
|
+
url: `${this.config.baseURL}/api/chat`,
|
|
147
|
+
headers: this.config.headers(),
|
|
148
|
+
body: {
|
|
149
|
+
...args,
|
|
150
|
+
stream: true
|
|
151
|
+
},
|
|
152
|
+
failedResponseHandler: inkeepFailedResponseHandler,
|
|
153
|
+
successfulResponseHandler: createEventSourceResponseHandler(StreamEventSchema),
|
|
154
|
+
abortSignal: options.abortSignal,
|
|
155
|
+
fetch: this.config.fetch
|
|
156
|
+
});
|
|
157
|
+
const { value: stream } = response;
|
|
158
|
+
let finishReason = "unknown";
|
|
159
|
+
let usage = {
|
|
160
|
+
promptTokens: 0,
|
|
161
|
+
completionTokens: 0,
|
|
162
|
+
inputTokens: 0,
|
|
163
|
+
outputTokens: 0,
|
|
164
|
+
totalTokens: 0
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
stream: stream.pipeThrough(new TransformStream({
|
|
168
|
+
start(controller) {
|
|
169
|
+
controller.enqueue({
|
|
170
|
+
type: "stream-start",
|
|
171
|
+
warnings
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
transform(chunk, controller) {
|
|
175
|
+
if (!chunk.success) {
|
|
176
|
+
controller.enqueue({
|
|
177
|
+
type: "error",
|
|
178
|
+
error: chunk.error
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const event = chunk.value;
|
|
183
|
+
switch (event.type) {
|
|
184
|
+
case "text-start":
|
|
185
|
+
controller.enqueue({
|
|
186
|
+
type: "text-start",
|
|
187
|
+
id: event.id
|
|
188
|
+
});
|
|
189
|
+
break;
|
|
190
|
+
case "text-delta":
|
|
191
|
+
controller.enqueue({
|
|
192
|
+
type: "text-delta",
|
|
193
|
+
id: event.id,
|
|
194
|
+
delta: event.delta
|
|
195
|
+
});
|
|
196
|
+
break;
|
|
197
|
+
case "text-end":
|
|
198
|
+
controller.enqueue({
|
|
199
|
+
type: "text-end",
|
|
200
|
+
id: event.id
|
|
201
|
+
});
|
|
202
|
+
break;
|
|
203
|
+
case "data-component":
|
|
204
|
+
case "data-summary": break;
|
|
205
|
+
case "data-operation": {
|
|
206
|
+
const toolEvent = convertDataOperationToToolEvent(event.data);
|
|
207
|
+
if (toolEvent) controller.enqueue(toolEvent);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
case "error":
|
|
211
|
+
controller.enqueue({
|
|
212
|
+
type: "error",
|
|
213
|
+
error: new Error(event.error)
|
|
214
|
+
});
|
|
215
|
+
break;
|
|
216
|
+
case "finish":
|
|
217
|
+
if (event.finishReason) finishReason = mapInkeepFinishReason(event.finishReason);
|
|
218
|
+
if (event.usage) usage = {
|
|
219
|
+
promptTokens: event.usage.promptTokens ?? 0,
|
|
220
|
+
completionTokens: event.usage.completionTokens ?? 0,
|
|
221
|
+
inputTokens: event.usage.promptTokens ?? 0,
|
|
222
|
+
outputTokens: event.usage.completionTokens ?? 0,
|
|
223
|
+
totalTokens: event.usage.totalTokens ?? 0
|
|
224
|
+
};
|
|
225
|
+
break;
|
|
226
|
+
default:
|
|
227
|
+
console.warn("Unhandled stream event type");
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
flush(controller) {
|
|
232
|
+
controller.enqueue({
|
|
233
|
+
type: "finish",
|
|
234
|
+
finishReason,
|
|
235
|
+
usage
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
})),
|
|
239
|
+
rawCall: {
|
|
240
|
+
rawPrompt: args.messages,
|
|
241
|
+
rawSettings: args
|
|
242
|
+
},
|
|
243
|
+
rawResponse: { headers: response.responseHeaders },
|
|
244
|
+
warnings
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const inkeepChatCompletionSchema = z.object({
|
|
249
|
+
id: z.string(),
|
|
250
|
+
object: z.literal("chat.completion"),
|
|
251
|
+
created: z.number(),
|
|
252
|
+
model: z.string().optional(),
|
|
253
|
+
choices: z.array(z.object({
|
|
254
|
+
index: z.number(),
|
|
255
|
+
message: z.object({
|
|
256
|
+
role: z.literal("assistant"),
|
|
257
|
+
content: z.string()
|
|
258
|
+
}),
|
|
259
|
+
finish_reason: z.string()
|
|
260
|
+
})),
|
|
261
|
+
usage: z.object({
|
|
262
|
+
prompt_tokens: z.number().optional(),
|
|
263
|
+
completion_tokens: z.number().optional(),
|
|
264
|
+
total_tokens: z.number().optional()
|
|
265
|
+
}).optional()
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
export { InkeepChatLanguageModel, convertDataOperationToToolEvent };
|
|
File without changes
|