@getpochi/cli 0.5.60 → 0.5.61
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/cli.js +620 -98
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -347749,6 +347749,520 @@ var originalGenerateId = createIdGenerator({
|
|
|
347749
347749
|
prefix: "aitxt",
|
|
347750
347750
|
size: 24
|
|
347751
347751
|
});
|
|
347752
|
+
async function generateText({
|
|
347753
|
+
model: modelArg,
|
|
347754
|
+
tools,
|
|
347755
|
+
toolChoice,
|
|
347756
|
+
system,
|
|
347757
|
+
prompt,
|
|
347758
|
+
messages,
|
|
347759
|
+
maxRetries: maxRetriesArg,
|
|
347760
|
+
abortSignal,
|
|
347761
|
+
headers,
|
|
347762
|
+
stopWhen = stepCountIs(1),
|
|
347763
|
+
experimental_output: output,
|
|
347764
|
+
experimental_telemetry: telemetry,
|
|
347765
|
+
providerOptions,
|
|
347766
|
+
experimental_activeTools,
|
|
347767
|
+
activeTools = experimental_activeTools,
|
|
347768
|
+
experimental_prepareStep,
|
|
347769
|
+
prepareStep = experimental_prepareStep,
|
|
347770
|
+
experimental_repairToolCall: repairToolCall,
|
|
347771
|
+
experimental_download: download2,
|
|
347772
|
+
experimental_context,
|
|
347773
|
+
_internal: {
|
|
347774
|
+
generateId: generateId3 = originalGenerateId,
|
|
347775
|
+
currentDate = () => /* @__PURE__ */ new Date
|
|
347776
|
+
} = {},
|
|
347777
|
+
onStepFinish,
|
|
347778
|
+
...settings
|
|
347779
|
+
}) {
|
|
347780
|
+
const model = resolveLanguageModel(modelArg);
|
|
347781
|
+
const stopConditions = asArray(stopWhen);
|
|
347782
|
+
const { maxRetries, retry } = prepareRetries({
|
|
347783
|
+
maxRetries: maxRetriesArg,
|
|
347784
|
+
abortSignal
|
|
347785
|
+
});
|
|
347786
|
+
const callSettings = prepareCallSettings(settings);
|
|
347787
|
+
const headersWithUserAgent = withUserAgentSuffix(headers != null ? headers : {}, `ai/${VERSION3}`);
|
|
347788
|
+
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
347789
|
+
model,
|
|
347790
|
+
telemetry,
|
|
347791
|
+
headers: headersWithUserAgent,
|
|
347792
|
+
settings: { ...callSettings, maxRetries }
|
|
347793
|
+
});
|
|
347794
|
+
const initialPrompt = await standardizePrompt({
|
|
347795
|
+
system,
|
|
347796
|
+
prompt,
|
|
347797
|
+
messages
|
|
347798
|
+
});
|
|
347799
|
+
const tracer = getTracer(telemetry);
|
|
347800
|
+
try {
|
|
347801
|
+
return await recordSpan({
|
|
347802
|
+
name: "ai.generateText",
|
|
347803
|
+
attributes: selectTelemetryAttributes({
|
|
347804
|
+
telemetry,
|
|
347805
|
+
attributes: {
|
|
347806
|
+
...assembleOperationName({
|
|
347807
|
+
operationId: "ai.generateText",
|
|
347808
|
+
telemetry
|
|
347809
|
+
}),
|
|
347810
|
+
...baseTelemetryAttributes,
|
|
347811
|
+
"ai.model.provider": model.provider,
|
|
347812
|
+
"ai.model.id": model.modelId,
|
|
347813
|
+
"ai.prompt": {
|
|
347814
|
+
input: () => JSON.stringify({ system, prompt, messages })
|
|
347815
|
+
}
|
|
347816
|
+
}
|
|
347817
|
+
}),
|
|
347818
|
+
tracer,
|
|
347819
|
+
fn: async (span) => {
|
|
347820
|
+
var _a172, _b8, _c, _d, _e, _f, _g;
|
|
347821
|
+
const callSettings2 = prepareCallSettings(settings);
|
|
347822
|
+
let currentModelResponse;
|
|
347823
|
+
let clientToolCalls = [];
|
|
347824
|
+
let clientToolOutputs = [];
|
|
347825
|
+
const responseMessages = [];
|
|
347826
|
+
const steps = [];
|
|
347827
|
+
do {
|
|
347828
|
+
const stepInputMessages = [
|
|
347829
|
+
...initialPrompt.messages,
|
|
347830
|
+
...responseMessages
|
|
347831
|
+
];
|
|
347832
|
+
const prepareStepResult = await (prepareStep == null ? undefined : prepareStep({
|
|
347833
|
+
model,
|
|
347834
|
+
steps,
|
|
347835
|
+
stepNumber: steps.length,
|
|
347836
|
+
messages: stepInputMessages
|
|
347837
|
+
}));
|
|
347838
|
+
const promptMessages = await convertToLanguageModelPrompt({
|
|
347839
|
+
prompt: {
|
|
347840
|
+
system: (_a172 = prepareStepResult == null ? undefined : prepareStepResult.system) != null ? _a172 : initialPrompt.system,
|
|
347841
|
+
messages: (_b8 = prepareStepResult == null ? undefined : prepareStepResult.messages) != null ? _b8 : stepInputMessages
|
|
347842
|
+
},
|
|
347843
|
+
supportedUrls: await model.supportedUrls,
|
|
347844
|
+
download: download2
|
|
347845
|
+
});
|
|
347846
|
+
const stepModel = resolveLanguageModel((_c = prepareStepResult == null ? undefined : prepareStepResult.model) != null ? _c : model);
|
|
347847
|
+
const { toolChoice: stepToolChoice, tools: stepTools } = prepareToolsAndToolChoice({
|
|
347848
|
+
tools,
|
|
347849
|
+
toolChoice: (_d = prepareStepResult == null ? undefined : prepareStepResult.toolChoice) != null ? _d : toolChoice,
|
|
347850
|
+
activeTools: (_e = prepareStepResult == null ? undefined : prepareStepResult.activeTools) != null ? _e : activeTools
|
|
347851
|
+
});
|
|
347852
|
+
currentModelResponse = await retry(() => {
|
|
347853
|
+
var _a18;
|
|
347854
|
+
return recordSpan({
|
|
347855
|
+
name: "ai.generateText.doGenerate",
|
|
347856
|
+
attributes: selectTelemetryAttributes({
|
|
347857
|
+
telemetry,
|
|
347858
|
+
attributes: {
|
|
347859
|
+
...assembleOperationName({
|
|
347860
|
+
operationId: "ai.generateText.doGenerate",
|
|
347861
|
+
telemetry
|
|
347862
|
+
}),
|
|
347863
|
+
...baseTelemetryAttributes,
|
|
347864
|
+
"ai.model.provider": stepModel.provider,
|
|
347865
|
+
"ai.model.id": stepModel.modelId,
|
|
347866
|
+
"ai.prompt.messages": {
|
|
347867
|
+
input: () => stringifyForTelemetry(promptMessages)
|
|
347868
|
+
},
|
|
347869
|
+
"ai.prompt.tools": {
|
|
347870
|
+
input: () => stepTools == null ? undefined : stepTools.map((tool3) => JSON.stringify(tool3))
|
|
347871
|
+
},
|
|
347872
|
+
"ai.prompt.toolChoice": {
|
|
347873
|
+
input: () => stepToolChoice != null ? JSON.stringify(stepToolChoice) : undefined
|
|
347874
|
+
},
|
|
347875
|
+
"gen_ai.system": stepModel.provider,
|
|
347876
|
+
"gen_ai.request.model": stepModel.modelId,
|
|
347877
|
+
"gen_ai.request.frequency_penalty": settings.frequencyPenalty,
|
|
347878
|
+
"gen_ai.request.max_tokens": settings.maxOutputTokens,
|
|
347879
|
+
"gen_ai.request.presence_penalty": settings.presencePenalty,
|
|
347880
|
+
"gen_ai.request.stop_sequences": settings.stopSequences,
|
|
347881
|
+
"gen_ai.request.temperature": (_a18 = settings.temperature) != null ? _a18 : undefined,
|
|
347882
|
+
"gen_ai.request.top_k": settings.topK,
|
|
347883
|
+
"gen_ai.request.top_p": settings.topP
|
|
347884
|
+
}
|
|
347885
|
+
}),
|
|
347886
|
+
tracer,
|
|
347887
|
+
fn: async (span2) => {
|
|
347888
|
+
var _a19, _b22, _c2, _d2, _e2, _f2, _g2, _h;
|
|
347889
|
+
const result2 = await stepModel.doGenerate({
|
|
347890
|
+
...callSettings2,
|
|
347891
|
+
tools: stepTools,
|
|
347892
|
+
toolChoice: stepToolChoice,
|
|
347893
|
+
responseFormat: output == null ? undefined : output.responseFormat,
|
|
347894
|
+
prompt: promptMessages,
|
|
347895
|
+
providerOptions,
|
|
347896
|
+
abortSignal,
|
|
347897
|
+
headers: headersWithUserAgent
|
|
347898
|
+
});
|
|
347899
|
+
const responseData = {
|
|
347900
|
+
id: (_b22 = (_a19 = result2.response) == null ? undefined : _a19.id) != null ? _b22 : generateId3(),
|
|
347901
|
+
timestamp: (_d2 = (_c2 = result2.response) == null ? undefined : _c2.timestamp) != null ? _d2 : currentDate(),
|
|
347902
|
+
modelId: (_f2 = (_e2 = result2.response) == null ? undefined : _e2.modelId) != null ? _f2 : stepModel.modelId,
|
|
347903
|
+
headers: (_g2 = result2.response) == null ? undefined : _g2.headers,
|
|
347904
|
+
body: (_h = result2.response) == null ? undefined : _h.body
|
|
347905
|
+
};
|
|
347906
|
+
span2.setAttributes(selectTelemetryAttributes({
|
|
347907
|
+
telemetry,
|
|
347908
|
+
attributes: {
|
|
347909
|
+
"ai.response.finishReason": result2.finishReason,
|
|
347910
|
+
"ai.response.text": {
|
|
347911
|
+
output: () => extractTextContent(result2.content)
|
|
347912
|
+
},
|
|
347913
|
+
"ai.response.toolCalls": {
|
|
347914
|
+
output: () => {
|
|
347915
|
+
const toolCalls = asToolCalls(result2.content);
|
|
347916
|
+
return toolCalls == null ? undefined : JSON.stringify(toolCalls);
|
|
347917
|
+
}
|
|
347918
|
+
},
|
|
347919
|
+
"ai.response.id": responseData.id,
|
|
347920
|
+
"ai.response.model": responseData.modelId,
|
|
347921
|
+
"ai.response.timestamp": responseData.timestamp.toISOString(),
|
|
347922
|
+
"ai.response.providerMetadata": JSON.stringify(result2.providerMetadata),
|
|
347923
|
+
"ai.usage.promptTokens": result2.usage.inputTokens,
|
|
347924
|
+
"ai.usage.completionTokens": result2.usage.outputTokens,
|
|
347925
|
+
"gen_ai.response.finish_reasons": [result2.finishReason],
|
|
347926
|
+
"gen_ai.response.id": responseData.id,
|
|
347927
|
+
"gen_ai.response.model": responseData.modelId,
|
|
347928
|
+
"gen_ai.usage.input_tokens": result2.usage.inputTokens,
|
|
347929
|
+
"gen_ai.usage.output_tokens": result2.usage.outputTokens
|
|
347930
|
+
}
|
|
347931
|
+
}));
|
|
347932
|
+
return { ...result2, response: responseData };
|
|
347933
|
+
}
|
|
347934
|
+
});
|
|
347935
|
+
});
|
|
347936
|
+
const stepToolCalls = await Promise.all(currentModelResponse.content.filter((part) => part.type === "tool-call").map((toolCall) => parseToolCall({
|
|
347937
|
+
toolCall,
|
|
347938
|
+
tools,
|
|
347939
|
+
repairToolCall,
|
|
347940
|
+
system,
|
|
347941
|
+
messages: stepInputMessages
|
|
347942
|
+
})));
|
|
347943
|
+
for (const toolCall of stepToolCalls) {
|
|
347944
|
+
if (toolCall.invalid) {
|
|
347945
|
+
continue;
|
|
347946
|
+
}
|
|
347947
|
+
const tool3 = tools[toolCall.toolName];
|
|
347948
|
+
if ((tool3 == null ? undefined : tool3.onInputAvailable) != null) {
|
|
347949
|
+
await tool3.onInputAvailable({
|
|
347950
|
+
input: toolCall.input,
|
|
347951
|
+
toolCallId: toolCall.toolCallId,
|
|
347952
|
+
messages: stepInputMessages,
|
|
347953
|
+
abortSignal,
|
|
347954
|
+
experimental_context
|
|
347955
|
+
});
|
|
347956
|
+
}
|
|
347957
|
+
}
|
|
347958
|
+
const invalidToolCalls = stepToolCalls.filter((toolCall) => toolCall.invalid && toolCall.dynamic);
|
|
347959
|
+
clientToolOutputs = [];
|
|
347960
|
+
for (const toolCall of invalidToolCalls) {
|
|
347961
|
+
clientToolOutputs.push({
|
|
347962
|
+
type: "tool-error",
|
|
347963
|
+
toolCallId: toolCall.toolCallId,
|
|
347964
|
+
toolName: toolCall.toolName,
|
|
347965
|
+
input: toolCall.input,
|
|
347966
|
+
error: getErrorMessage2(toolCall.error),
|
|
347967
|
+
dynamic: true
|
|
347968
|
+
});
|
|
347969
|
+
}
|
|
347970
|
+
clientToolCalls = stepToolCalls.filter((toolCall) => !toolCall.providerExecuted);
|
|
347971
|
+
if (tools != null) {
|
|
347972
|
+
clientToolOutputs.push(...await executeTools({
|
|
347973
|
+
toolCalls: clientToolCalls.filter((toolCall) => !toolCall.invalid),
|
|
347974
|
+
tools,
|
|
347975
|
+
tracer,
|
|
347976
|
+
telemetry,
|
|
347977
|
+
messages: stepInputMessages,
|
|
347978
|
+
abortSignal,
|
|
347979
|
+
experimental_context
|
|
347980
|
+
}));
|
|
347981
|
+
}
|
|
347982
|
+
const stepContent = asContent({
|
|
347983
|
+
content: currentModelResponse.content,
|
|
347984
|
+
toolCalls: stepToolCalls,
|
|
347985
|
+
toolOutputs: clientToolOutputs
|
|
347986
|
+
});
|
|
347987
|
+
responseMessages.push(...toResponseMessages({
|
|
347988
|
+
content: stepContent,
|
|
347989
|
+
tools
|
|
347990
|
+
}));
|
|
347991
|
+
const currentStepResult = new DefaultStepResult({
|
|
347992
|
+
content: stepContent,
|
|
347993
|
+
finishReason: currentModelResponse.finishReason,
|
|
347994
|
+
usage: currentModelResponse.usage,
|
|
347995
|
+
warnings: currentModelResponse.warnings,
|
|
347996
|
+
providerMetadata: currentModelResponse.providerMetadata,
|
|
347997
|
+
request: (_f = currentModelResponse.request) != null ? _f : {},
|
|
347998
|
+
response: {
|
|
347999
|
+
...currentModelResponse.response,
|
|
348000
|
+
messages: structuredClone(responseMessages)
|
|
348001
|
+
}
|
|
348002
|
+
});
|
|
348003
|
+
logWarnings((_g = currentModelResponse.warnings) != null ? _g : []);
|
|
348004
|
+
steps.push(currentStepResult);
|
|
348005
|
+
await (onStepFinish == null ? undefined : onStepFinish(currentStepResult));
|
|
348006
|
+
} while (clientToolCalls.length > 0 && clientToolOutputs.length === clientToolCalls.length && !await isStopConditionMet({ stopConditions, steps }));
|
|
348007
|
+
span.setAttributes(selectTelemetryAttributes({
|
|
348008
|
+
telemetry,
|
|
348009
|
+
attributes: {
|
|
348010
|
+
"ai.response.finishReason": currentModelResponse.finishReason,
|
|
348011
|
+
"ai.response.text": {
|
|
348012
|
+
output: () => extractTextContent(currentModelResponse.content)
|
|
348013
|
+
},
|
|
348014
|
+
"ai.response.toolCalls": {
|
|
348015
|
+
output: () => {
|
|
348016
|
+
const toolCalls = asToolCalls(currentModelResponse.content);
|
|
348017
|
+
return toolCalls == null ? undefined : JSON.stringify(toolCalls);
|
|
348018
|
+
}
|
|
348019
|
+
},
|
|
348020
|
+
"ai.response.providerMetadata": JSON.stringify(currentModelResponse.providerMetadata),
|
|
348021
|
+
"ai.usage.promptTokens": currentModelResponse.usage.inputTokens,
|
|
348022
|
+
"ai.usage.completionTokens": currentModelResponse.usage.outputTokens
|
|
348023
|
+
}
|
|
348024
|
+
}));
|
|
348025
|
+
const lastStep = steps[steps.length - 1];
|
|
348026
|
+
return new DefaultGenerateTextResult({
|
|
348027
|
+
steps,
|
|
348028
|
+
resolvedOutput: await (output == null ? undefined : output.parseOutput({ text: lastStep.text }, {
|
|
348029
|
+
response: lastStep.response,
|
|
348030
|
+
usage: lastStep.usage,
|
|
348031
|
+
finishReason: lastStep.finishReason
|
|
348032
|
+
}))
|
|
348033
|
+
});
|
|
348034
|
+
}
|
|
348035
|
+
});
|
|
348036
|
+
} catch (error40) {
|
|
348037
|
+
throw wrapGatewayError(error40);
|
|
348038
|
+
}
|
|
348039
|
+
}
|
|
348040
|
+
async function executeTools({
|
|
348041
|
+
toolCalls,
|
|
348042
|
+
tools,
|
|
348043
|
+
tracer,
|
|
348044
|
+
telemetry,
|
|
348045
|
+
messages,
|
|
348046
|
+
abortSignal,
|
|
348047
|
+
experimental_context
|
|
348048
|
+
}) {
|
|
348049
|
+
const toolOutputs = await Promise.all(toolCalls.map(async ({ toolCallId, toolName, input }) => {
|
|
348050
|
+
const tool3 = tools[toolName];
|
|
348051
|
+
if ((tool3 == null ? undefined : tool3.execute) == null) {
|
|
348052
|
+
return;
|
|
348053
|
+
}
|
|
348054
|
+
return recordSpan({
|
|
348055
|
+
name: "ai.toolCall",
|
|
348056
|
+
attributes: selectTelemetryAttributes({
|
|
348057
|
+
telemetry,
|
|
348058
|
+
attributes: {
|
|
348059
|
+
...assembleOperationName({
|
|
348060
|
+
operationId: "ai.toolCall",
|
|
348061
|
+
telemetry
|
|
348062
|
+
}),
|
|
348063
|
+
"ai.toolCall.name": toolName,
|
|
348064
|
+
"ai.toolCall.id": toolCallId,
|
|
348065
|
+
"ai.toolCall.args": {
|
|
348066
|
+
output: () => JSON.stringify(input)
|
|
348067
|
+
}
|
|
348068
|
+
}
|
|
348069
|
+
}),
|
|
348070
|
+
tracer,
|
|
348071
|
+
fn: async (span) => {
|
|
348072
|
+
try {
|
|
348073
|
+
const stream = executeTool({
|
|
348074
|
+
execute: tool3.execute.bind(tool3),
|
|
348075
|
+
input,
|
|
348076
|
+
options: {
|
|
348077
|
+
toolCallId,
|
|
348078
|
+
messages,
|
|
348079
|
+
abortSignal,
|
|
348080
|
+
experimental_context
|
|
348081
|
+
}
|
|
348082
|
+
});
|
|
348083
|
+
let output;
|
|
348084
|
+
for await (const part of stream) {
|
|
348085
|
+
if (part.type === "final") {
|
|
348086
|
+
output = part.output;
|
|
348087
|
+
}
|
|
348088
|
+
}
|
|
348089
|
+
try {
|
|
348090
|
+
span.setAttributes(selectTelemetryAttributes({
|
|
348091
|
+
telemetry,
|
|
348092
|
+
attributes: {
|
|
348093
|
+
"ai.toolCall.result": {
|
|
348094
|
+
output: () => JSON.stringify(output)
|
|
348095
|
+
}
|
|
348096
|
+
}
|
|
348097
|
+
}));
|
|
348098
|
+
} catch (ignored) {}
|
|
348099
|
+
return {
|
|
348100
|
+
type: "tool-result",
|
|
348101
|
+
toolCallId,
|
|
348102
|
+
toolName,
|
|
348103
|
+
input,
|
|
348104
|
+
output,
|
|
348105
|
+
dynamic: tool3.type === "dynamic"
|
|
348106
|
+
};
|
|
348107
|
+
} catch (error40) {
|
|
348108
|
+
recordErrorOnSpan(span, error40);
|
|
348109
|
+
return {
|
|
348110
|
+
type: "tool-error",
|
|
348111
|
+
toolCallId,
|
|
348112
|
+
toolName,
|
|
348113
|
+
input,
|
|
348114
|
+
error: error40,
|
|
348115
|
+
dynamic: tool3.type === "dynamic"
|
|
348116
|
+
};
|
|
348117
|
+
}
|
|
348118
|
+
}
|
|
348119
|
+
});
|
|
348120
|
+
}));
|
|
348121
|
+
return toolOutputs.filter((output) => output != null);
|
|
348122
|
+
}
|
|
348123
|
+
var DefaultGenerateTextResult = class {
|
|
348124
|
+
constructor(options) {
|
|
348125
|
+
this.steps = options.steps;
|
|
348126
|
+
this.resolvedOutput = options.resolvedOutput;
|
|
348127
|
+
}
|
|
348128
|
+
get finalStep() {
|
|
348129
|
+
return this.steps[this.steps.length - 1];
|
|
348130
|
+
}
|
|
348131
|
+
get content() {
|
|
348132
|
+
return this.finalStep.content;
|
|
348133
|
+
}
|
|
348134
|
+
get text() {
|
|
348135
|
+
return this.finalStep.text;
|
|
348136
|
+
}
|
|
348137
|
+
get files() {
|
|
348138
|
+
return this.finalStep.files;
|
|
348139
|
+
}
|
|
348140
|
+
get reasoningText() {
|
|
348141
|
+
return this.finalStep.reasoningText;
|
|
348142
|
+
}
|
|
348143
|
+
get reasoning() {
|
|
348144
|
+
return this.finalStep.reasoning;
|
|
348145
|
+
}
|
|
348146
|
+
get toolCalls() {
|
|
348147
|
+
return this.finalStep.toolCalls;
|
|
348148
|
+
}
|
|
348149
|
+
get staticToolCalls() {
|
|
348150
|
+
return this.finalStep.staticToolCalls;
|
|
348151
|
+
}
|
|
348152
|
+
get dynamicToolCalls() {
|
|
348153
|
+
return this.finalStep.dynamicToolCalls;
|
|
348154
|
+
}
|
|
348155
|
+
get toolResults() {
|
|
348156
|
+
return this.finalStep.toolResults;
|
|
348157
|
+
}
|
|
348158
|
+
get staticToolResults() {
|
|
348159
|
+
return this.finalStep.staticToolResults;
|
|
348160
|
+
}
|
|
348161
|
+
get dynamicToolResults() {
|
|
348162
|
+
return this.finalStep.dynamicToolResults;
|
|
348163
|
+
}
|
|
348164
|
+
get sources() {
|
|
348165
|
+
return this.finalStep.sources;
|
|
348166
|
+
}
|
|
348167
|
+
get finishReason() {
|
|
348168
|
+
return this.finalStep.finishReason;
|
|
348169
|
+
}
|
|
348170
|
+
get warnings() {
|
|
348171
|
+
return this.finalStep.warnings;
|
|
348172
|
+
}
|
|
348173
|
+
get providerMetadata() {
|
|
348174
|
+
return this.finalStep.providerMetadata;
|
|
348175
|
+
}
|
|
348176
|
+
get response() {
|
|
348177
|
+
return this.finalStep.response;
|
|
348178
|
+
}
|
|
348179
|
+
get request() {
|
|
348180
|
+
return this.finalStep.request;
|
|
348181
|
+
}
|
|
348182
|
+
get usage() {
|
|
348183
|
+
return this.finalStep.usage;
|
|
348184
|
+
}
|
|
348185
|
+
get totalUsage() {
|
|
348186
|
+
return this.steps.reduce((totalUsage, step) => {
|
|
348187
|
+
return addLanguageModelUsage(totalUsage, step.usage);
|
|
348188
|
+
}, {
|
|
348189
|
+
inputTokens: undefined,
|
|
348190
|
+
outputTokens: undefined,
|
|
348191
|
+
totalTokens: undefined,
|
|
348192
|
+
reasoningTokens: undefined,
|
|
348193
|
+
cachedInputTokens: undefined
|
|
348194
|
+
});
|
|
348195
|
+
}
|
|
348196
|
+
get experimental_output() {
|
|
348197
|
+
if (this.resolvedOutput == null) {
|
|
348198
|
+
throw new NoOutputSpecifiedError;
|
|
348199
|
+
}
|
|
348200
|
+
return this.resolvedOutput;
|
|
348201
|
+
}
|
|
348202
|
+
};
|
|
348203
|
+
function asToolCalls(content) {
|
|
348204
|
+
const parts = content.filter((part) => part.type === "tool-call");
|
|
348205
|
+
if (parts.length === 0) {
|
|
348206
|
+
return;
|
|
348207
|
+
}
|
|
348208
|
+
return parts.map((toolCall) => ({
|
|
348209
|
+
toolCallId: toolCall.toolCallId,
|
|
348210
|
+
toolName: toolCall.toolName,
|
|
348211
|
+
input: toolCall.input
|
|
348212
|
+
}));
|
|
348213
|
+
}
|
|
348214
|
+
function asContent({
|
|
348215
|
+
content,
|
|
348216
|
+
toolCalls,
|
|
348217
|
+
toolOutputs
|
|
348218
|
+
}) {
|
|
348219
|
+
return [
|
|
348220
|
+
...content.map((part) => {
|
|
348221
|
+
switch (part.type) {
|
|
348222
|
+
case "text":
|
|
348223
|
+
case "reasoning":
|
|
348224
|
+
case "source":
|
|
348225
|
+
return part;
|
|
348226
|
+
case "file": {
|
|
348227
|
+
return {
|
|
348228
|
+
type: "file",
|
|
348229
|
+
file: new DefaultGeneratedFile(part)
|
|
348230
|
+
};
|
|
348231
|
+
}
|
|
348232
|
+
case "tool-call": {
|
|
348233
|
+
return toolCalls.find((toolCall) => toolCall.toolCallId === part.toolCallId);
|
|
348234
|
+
}
|
|
348235
|
+
case "tool-result": {
|
|
348236
|
+
const toolCall = toolCalls.find((toolCall2) => toolCall2.toolCallId === part.toolCallId);
|
|
348237
|
+
if (toolCall == null) {
|
|
348238
|
+
throw new Error(`Tool call ${part.toolCallId} not found.`);
|
|
348239
|
+
}
|
|
348240
|
+
if (part.isError) {
|
|
348241
|
+
return {
|
|
348242
|
+
type: "tool-error",
|
|
348243
|
+
toolCallId: part.toolCallId,
|
|
348244
|
+
toolName: part.toolName,
|
|
348245
|
+
input: toolCall.input,
|
|
348246
|
+
error: part.result,
|
|
348247
|
+
providerExecuted: true,
|
|
348248
|
+
dynamic: toolCall.dynamic
|
|
348249
|
+
};
|
|
348250
|
+
}
|
|
348251
|
+
return {
|
|
348252
|
+
type: "tool-result",
|
|
348253
|
+
toolCallId: part.toolCallId,
|
|
348254
|
+
toolName: part.toolName,
|
|
348255
|
+
input: toolCall.input,
|
|
348256
|
+
output: part.result,
|
|
348257
|
+
providerExecuted: true,
|
|
348258
|
+
dynamic: toolCall.dynamic
|
|
348259
|
+
};
|
|
348260
|
+
}
|
|
348261
|
+
}
|
|
348262
|
+
}),
|
|
348263
|
+
...toolOutputs
|
|
348264
|
+
];
|
|
348265
|
+
}
|
|
347752
348266
|
function prepareHeaders(headers, defaultHeaders) {
|
|
347753
348267
|
const responseHeaders = new Headers(headers != null ? headers : {});
|
|
347754
348268
|
for (const [key, value2] of Object.entries(defaultHeaders)) {
|
|
@@ -359465,7 +359979,6 @@ function createModel(vendorId, opts) {
|
|
|
359465
359979
|
|
|
359466
359980
|
// ../vendor-pochi/src/model.ts
|
|
359467
359981
|
function createPochiModel({
|
|
359468
|
-
id,
|
|
359469
359982
|
modelId,
|
|
359470
359983
|
getCredentials
|
|
359471
359984
|
}) {
|
|
@@ -359474,11 +359987,28 @@ function createPochiModel({
|
|
|
359474
359987
|
provider: "pochi",
|
|
359475
359988
|
modelId: modelId || "<default>",
|
|
359476
359989
|
supportedUrls: {},
|
|
359477
|
-
doGenerate: async () =>
|
|
359478
|
-
|
|
359990
|
+
doGenerate: async ({ headers, ...options }) => {
|
|
359991
|
+
const apiClient = createApiClient(getCredentials);
|
|
359992
|
+
const resp = await apiClient.api.chat.$post({
|
|
359993
|
+
json: {
|
|
359994
|
+
model: modelId,
|
|
359995
|
+
options
|
|
359996
|
+
}
|
|
359997
|
+
}, {
|
|
359998
|
+
headers: headers ? i5(headers, (x) => x || "") : undefined
|
|
359999
|
+
});
|
|
360000
|
+
const data = await resp.json();
|
|
360001
|
+
return data;
|
|
360002
|
+
},
|
|
360003
|
+
doStream: async ({
|
|
360004
|
+
prompt,
|
|
360005
|
+
abortSignal,
|
|
360006
|
+
stopSequences,
|
|
360007
|
+
tools,
|
|
360008
|
+
headers
|
|
360009
|
+
}) => {
|
|
359479
360010
|
const apiClient = createApiClient(getCredentials);
|
|
359480
360011
|
const data = {
|
|
359481
|
-
id,
|
|
359482
360012
|
model: modelId,
|
|
359483
360013
|
callOptions: {
|
|
359484
360014
|
prompt: convertFilePartDataToBase64(prompt),
|
|
@@ -359489,6 +360019,7 @@ function createPochiModel({
|
|
|
359489
360019
|
const resp = await apiClient.api.chat.stream.$post({
|
|
359490
360020
|
json: data
|
|
359491
360021
|
}, {
|
|
360022
|
+
headers: headers ? i5(headers, (x) => x || "") : undefined,
|
|
359492
360023
|
init: {
|
|
359493
360024
|
signal: abortSignal
|
|
359494
360025
|
}
|
|
@@ -368440,7 +368971,7 @@ var {
|
|
|
368440
368971
|
// package.json
|
|
368441
368972
|
var package_default = {
|
|
368442
368973
|
name: "@getpochi/cli",
|
|
368443
|
-
version: "0.5.
|
|
368974
|
+
version: "0.5.61",
|
|
368444
368975
|
type: "module",
|
|
368445
368976
|
bin: {
|
|
368446
368977
|
pochi: "src/cli.ts"
|
|
@@ -409842,6 +410373,9 @@ function toTaskError(error46) {
|
|
|
409842
410373
|
return internalError(error46.message);
|
|
409843
410374
|
}
|
|
409844
410375
|
|
|
410376
|
+
// ../common/src/pochi-api.ts
|
|
410377
|
+
var PochiTaskIdHeader = "x-pochi-task-id";
|
|
410378
|
+
|
|
409845
410379
|
// ../livekit/src/chat/llm/generate-task-title.ts
|
|
409846
410380
|
var logger18 = getLogger("generateTaskTitle");
|
|
409847
410381
|
async function generateTaskTitle(options6) {
|
|
@@ -409853,6 +410387,7 @@ async function generateTaskTitle(options6) {
|
|
|
409853
410387
|
return newTitle;
|
|
409854
410388
|
}
|
|
409855
410389
|
async function generateTaskTitleImpl({
|
|
410390
|
+
taskId,
|
|
409856
410391
|
title,
|
|
409857
410392
|
messages: messages2,
|
|
409858
410393
|
getModel,
|
|
@@ -409870,7 +410405,7 @@ async function generateTaskTitleImpl({
|
|
|
409870
410405
|
if (partCount >= 5 && partCount < 20 && !isTitleGeneratedByLlm(title, titleFromMessages)) {
|
|
409871
410406
|
try {
|
|
409872
410407
|
const model2 = getModel();
|
|
409873
|
-
const title2 = await generateTitle2(model2, messages2, abortSignal);
|
|
410408
|
+
const title2 = await generateTitle2(taskId, model2, messages2, abortSignal);
|
|
409874
410409
|
if (title2.length > 0) {
|
|
409875
410410
|
return title2;
|
|
409876
410411
|
}
|
|
@@ -409896,7 +410431,7 @@ function getTitleFromMessages(messages2) {
|
|
|
409896
410431
|
function isTitleGeneratedByLlm(title, titleFromMessages) {
|
|
409897
410432
|
return title !== titleFromMessages;
|
|
409898
410433
|
}
|
|
409899
|
-
async function generateTitle2(model2, inputMessages, abortSignal) {
|
|
410434
|
+
async function generateTitle2(taskId, model2, inputMessages, abortSignal) {
|
|
409900
410435
|
const messages2 = [
|
|
409901
410436
|
...inputMessages,
|
|
409902
410437
|
{
|
|
@@ -409910,14 +410445,17 @@ async function generateTitle2(model2, inputMessages, abortSignal) {
|
|
|
409910
410445
|
]
|
|
409911
410446
|
}
|
|
409912
410447
|
];
|
|
409913
|
-
const
|
|
410448
|
+
const resp = await generateText({
|
|
410449
|
+
headers: {
|
|
410450
|
+
[PochiTaskIdHeader]: taskId
|
|
410451
|
+
},
|
|
409914
410452
|
model: model2,
|
|
409915
410453
|
prompt: convertToModelMessages(formatters.llm(messages2, { removeSystemReminder: true })),
|
|
409916
410454
|
abortSignal,
|
|
409917
410455
|
maxOutputTokens: 50,
|
|
409918
410456
|
maxRetries: 0
|
|
409919
410457
|
});
|
|
409920
|
-
return
|
|
410458
|
+
return resp.text;
|
|
409921
410459
|
}
|
|
409922
410460
|
|
|
409923
410461
|
// ../livekit/src/chat/background-job/manager.ts
|
|
@@ -409964,6 +410502,7 @@ async function process16({
|
|
|
409964
410502
|
return;
|
|
409965
410503
|
}
|
|
409966
410504
|
const newTitle = await generateTaskTitle({
|
|
410505
|
+
taskId,
|
|
409967
410506
|
title: task.title,
|
|
409968
410507
|
messages: messages2,
|
|
409969
410508
|
getModel
|
|
@@ -410050,56 +410589,33 @@ async function digest(data) {
|
|
|
410050
410589
|
}
|
|
410051
410590
|
|
|
410052
410591
|
// ../livekit/src/chat/llm/repair-tool-call.ts
|
|
410053
|
-
var
|
|
410054
|
-
var makeRepairToolCall = (model2) => async ({ toolCall, inputSchema: inputSchema2, error: error46 }) => {
|
|
410592
|
+
var makeRepairToolCall = (taskId, model2) => async ({ toolCall, inputSchema: inputSchema2, error: error46 }) => {
|
|
410055
410593
|
if (NoSuchToolError.isInstance(error46)) {
|
|
410056
410594
|
return null;
|
|
410057
410595
|
}
|
|
410058
|
-
const
|
|
410596
|
+
const tools = createClientTools();
|
|
410597
|
+
const tool2 = tools[toolCall.toolName];
|
|
410598
|
+
const { object: repairedArgs } = await generateObject({
|
|
410599
|
+
headers: {
|
|
410600
|
+
[PochiTaskIdHeader]: taskId
|
|
410601
|
+
},
|
|
410059
410602
|
model: model2,
|
|
410603
|
+
schema: tool2.inputSchema,
|
|
410060
410604
|
prompt: [
|
|
410061
|
-
`The model tried to call the tool "${toolCall.toolName}" with the following
|
|
410605
|
+
`The model tried to call the tool "${toolCall.toolName}" with the following inputs:`,
|
|
410062
410606
|
JSON.stringify(toolCall.input),
|
|
410063
410607
|
"The tool accepts the following schema:",
|
|
410064
410608
|
JSON.stringify(inputSchema2(toolCall)),
|
|
410065
|
-
"Please fix the
|
|
410066
|
-
"",
|
|
410067
|
-
"<good-example>",
|
|
410068
|
-
'{"path": "./src/file.ts", "content": "console.log("hello");"}',
|
|
410069
|
-
"</good-example>",
|
|
410070
|
-
"",
|
|
410071
|
-
"<bad-example>",
|
|
410072
|
-
"```json",
|
|
410073
|
-
'{"path": "./src/file.ts", "content": "console.log("hello");"}',
|
|
410074
|
-
"```",
|
|
410075
|
-
"</bad-example>",
|
|
410076
|
-
"",
|
|
410077
|
-
"<bad-example>",
|
|
410078
|
-
"Here is the corrected JSON:",
|
|
410079
|
-
'{"path": "./src/file.ts", "content": "console.log("hello");"}',
|
|
410080
|
-
"</bad-example>",
|
|
410081
|
-
"",
|
|
410082
|
-
"<bad-example>",
|
|
410083
|
-
"```",
|
|
410084
|
-
'{"path": "./src/file.ts", "content": "console.log("hello");"}',
|
|
410085
|
-
"```",
|
|
410086
|
-
"</bad-example>",
|
|
410087
|
-
"",
|
|
410088
|
-
"<bad-example>",
|
|
410089
|
-
'{"path": "./src/file.ts", "content": "console.log("hello");"} // Fixed arguments',
|
|
410090
|
-
"</bad-example>"
|
|
410609
|
+
"Please fix the inputs."
|
|
410091
410610
|
].join(`
|
|
410092
|
-
`)
|
|
410093
|
-
maxOutputTokens: 3000,
|
|
410094
|
-
maxRetries: 0
|
|
410611
|
+
`)
|
|
410095
410612
|
});
|
|
410096
|
-
|
|
410097
|
-
logger21.debug("Repairing tool call:", toolCall.toolName, input2);
|
|
410098
|
-
return { ...toolCall, input: input2 };
|
|
410613
|
+
return { ...toolCall, input: JSON.stringify(repairedArgs) };
|
|
410099
410614
|
};
|
|
410100
410615
|
// ../livekit/src/chat/llm/compact-task.ts
|
|
410101
|
-
var
|
|
410616
|
+
var logger21 = getLogger("compactTask");
|
|
410102
410617
|
async function compactTask({
|
|
410618
|
+
taskId,
|
|
410103
410619
|
model: model2,
|
|
410104
410620
|
messages: messages2,
|
|
410105
410621
|
abortSignal,
|
|
@@ -410110,7 +410626,7 @@ async function compactTask({
|
|
|
410110
410626
|
throw new Error("No messages to compact");
|
|
410111
410627
|
}
|
|
410112
410628
|
try {
|
|
410113
|
-
const text20 = prompts.inlineCompact(await createSummary(model2, abortSignal, messages2.slice(0, -1)), messages2.length - 1);
|
|
410629
|
+
const text20 = prompts.inlineCompact(await createSummary(taskId, model2, abortSignal, messages2.slice(0, -1)), messages2.length - 1);
|
|
410114
410630
|
if (inline) {
|
|
410115
410631
|
lastMessage.parts.unshift({
|
|
410116
410632
|
type: "text",
|
|
@@ -410120,10 +410636,10 @@ async function compactTask({
|
|
|
410120
410636
|
}
|
|
410121
410637
|
return text20;
|
|
410122
410638
|
} catch (err2) {
|
|
410123
|
-
|
|
410639
|
+
logger21.warn("Failed to create summary", err2);
|
|
410124
410640
|
}
|
|
410125
410641
|
}
|
|
410126
|
-
async function createSummary(model2, abortSignal, inputMessages) {
|
|
410642
|
+
async function createSummary(taskId, model2, abortSignal, inputMessages) {
|
|
410127
410643
|
const messages2 = [
|
|
410128
410644
|
...inputMessages,
|
|
410129
410645
|
{
|
|
@@ -410137,7 +410653,10 @@ async function createSummary(model2, abortSignal, inputMessages) {
|
|
|
410137
410653
|
]
|
|
410138
410654
|
}
|
|
410139
410655
|
];
|
|
410140
|
-
const
|
|
410656
|
+
const resp = await generateText({
|
|
410657
|
+
headers: {
|
|
410658
|
+
[PochiTaskIdHeader]: taskId
|
|
410659
|
+
},
|
|
410141
410660
|
model: model2,
|
|
410142
410661
|
prompt: convertToModelMessages(formatters.llm(messages2, {
|
|
410143
410662
|
removeSystemReminder: true
|
|
@@ -410146,7 +410665,7 @@ async function createSummary(model2, abortSignal, inputMessages) {
|
|
|
410146
410665
|
maxOutputTokens: 3000,
|
|
410147
410666
|
maxRetries: 0
|
|
410148
410667
|
});
|
|
410149
|
-
return
|
|
410668
|
+
return resp.text;
|
|
410150
410669
|
}
|
|
410151
410670
|
// ../livekit/src/chat/mcp-utils.ts
|
|
410152
410671
|
function parseMcpToolSet(store, mcpToolSet) {
|
|
@@ -410871,10 +411390,10 @@ function createStopWordStream(stop3) {
|
|
|
410871
411390
|
});
|
|
410872
411391
|
}
|
|
410873
411392
|
// ../livekit/src/chat/middlewares/output-schema-middleware.ts
|
|
410874
|
-
function createOutputSchemaMiddleware(outputSchema2) {
|
|
411393
|
+
function createOutputSchemaMiddleware(taskId, model2, outputSchema2) {
|
|
410875
411394
|
return {
|
|
410876
411395
|
middlewareVersion: "v2",
|
|
410877
|
-
wrapStream: async ({ doStream
|
|
411396
|
+
wrapStream: async ({ doStream }) => {
|
|
410878
411397
|
const { stream: stream12, ...rest } = await doStream();
|
|
410879
411398
|
let toolCallId = "";
|
|
410880
411399
|
const transformedStream = stream12.pipeThrough(new TransformStream({
|
|
@@ -410901,7 +411420,7 @@ function createOutputSchemaMiddleware(outputSchema2) {
|
|
|
410901
411420
|
const { result: result2 } = parsedResult.value;
|
|
410902
411421
|
const newInput = {
|
|
410903
411422
|
...parsedResult.value,
|
|
410904
|
-
result: await ensureOutputSchema(model2, outputSchema2, result2)
|
|
411423
|
+
result: await ensureOutputSchema(taskId, model2, outputSchema2, result2)
|
|
410905
411424
|
};
|
|
410906
411425
|
controller.enqueue({
|
|
410907
411426
|
...chunk4,
|
|
@@ -410920,9 +411439,12 @@ function createOutputSchemaMiddleware(outputSchema2) {
|
|
|
410920
411439
|
}
|
|
410921
411440
|
};
|
|
410922
411441
|
}
|
|
410923
|
-
async function ensureOutputSchema(model2, schema7, content3) {
|
|
411442
|
+
async function ensureOutputSchema(taskId, model2, schema7, content3) {
|
|
410924
411443
|
try {
|
|
410925
411444
|
const { object: object4 } = await generateObject({
|
|
411445
|
+
headers: {
|
|
411446
|
+
[PochiTaskIdHeader]: taskId
|
|
411447
|
+
},
|
|
410926
411448
|
model: model2,
|
|
410927
411449
|
schema: schema7,
|
|
410928
411450
|
prompt: [
|
|
@@ -411097,7 +411619,7 @@ function createGoogleVertexTuningModel(llm) {
|
|
|
411097
411619
|
}
|
|
411098
411620
|
|
|
411099
411621
|
// ../livekit/src/chat/models/openai.ts
|
|
411100
|
-
var
|
|
411622
|
+
var logger22 = getLogger("openai");
|
|
411101
411623
|
var OpenAIRequestParamsSchema = exports_external.object({
|
|
411102
411624
|
max_tokens: exports_external.number().positive().optional(),
|
|
411103
411625
|
max_completion_tokens: exports_external.number().positive().optional()
|
|
@@ -411147,7 +411669,7 @@ function overrideMaxOutputToken(body3) {
|
|
|
411147
411669
|
const json10 = JSON.parse(body3);
|
|
411148
411670
|
const result2 = OpenAIRequestParamsSchema.safeParse(json10);
|
|
411149
411671
|
if (!result2.success) {
|
|
411150
|
-
|
|
411672
|
+
logger22.error("OpenAI request body validation failed:", result2.error);
|
|
411151
411673
|
return;
|
|
411152
411674
|
}
|
|
411153
411675
|
const parsed = result2.data;
|
|
@@ -411156,7 +411678,7 @@ function overrideMaxOutputToken(body3) {
|
|
|
411156
411678
|
}
|
|
411157
411679
|
return JSON.stringify(parsed);
|
|
411158
411680
|
} catch (error46) {
|
|
411159
|
-
|
|
411681
|
+
logger22.error("Failed to parse OpenAI request body:", error46);
|
|
411160
411682
|
}
|
|
411161
411683
|
return;
|
|
411162
411684
|
}
|
|
@@ -411185,12 +411707,9 @@ function createOpenAIResponsesModel(llm) {
|
|
|
411185
411707
|
}
|
|
411186
411708
|
|
|
411187
411709
|
// ../livekit/src/chat/models/index.ts
|
|
411188
|
-
function createModel2({
|
|
411189
|
-
id: id5,
|
|
411190
|
-
llm
|
|
411191
|
-
}) {
|
|
411710
|
+
function createModel2({ llm }) {
|
|
411192
411711
|
if (llm.type === "vendor") {
|
|
411193
|
-
return llm.getModel(
|
|
411712
|
+
return llm.getModel();
|
|
411194
411713
|
}
|
|
411195
411714
|
if (llm.type === "openai") {
|
|
411196
411715
|
return createOpenAIModel(llm);
|
|
@@ -411246,6 +411765,7 @@ class FlexibleChatTransport {
|
|
|
411246
411765
|
abortSignal,
|
|
411247
411766
|
getters: this.getters
|
|
411248
411767
|
});
|
|
411768
|
+
const model2 = createModel2({ llm });
|
|
411249
411769
|
const middlewares = [];
|
|
411250
411770
|
if (!this.isSubTask) {
|
|
411251
411771
|
middlewares.push(createNewTaskMiddleware(this.store, environment2?.info.cwd, chatId, customAgents));
|
|
@@ -411254,7 +411774,7 @@ class FlexibleChatTransport {
|
|
|
411254
411774
|
middlewares.push(createReasoningMiddleware());
|
|
411255
411775
|
}
|
|
411256
411776
|
if (this.outputSchema) {
|
|
411257
|
-
middlewares.push(createOutputSchemaMiddleware(this.outputSchema));
|
|
411777
|
+
middlewares.push(createOutputSchemaMiddleware(chatId, model2, this.outputSchema));
|
|
411258
411778
|
}
|
|
411259
411779
|
if (llm.useToolCallMiddleware) {
|
|
411260
411780
|
middlewares.push(createToolCallMiddleware());
|
|
@@ -411274,8 +411794,10 @@ class FlexibleChatTransport {
|
|
|
411274
411794
|
return true;
|
|
411275
411795
|
});
|
|
411276
411796
|
const preparedMessages = await prepareMessages(messages2, environment2);
|
|
411277
|
-
const model2 = createModel2({ id: chatId, llm });
|
|
411278
411797
|
const stream12 = streamText({
|
|
411798
|
+
headers: {
|
|
411799
|
+
[PochiTaskIdHeader]: chatId
|
|
411800
|
+
},
|
|
411279
411801
|
system: prompts.system(environment2?.info?.customRules, this.customAgent, mcpInfo?.instructions),
|
|
411280
411802
|
messages: convertToModelMessages(formatters.llm(preparedMessages), { tools }),
|
|
411281
411803
|
model: wrapLanguageModel({
|
|
@@ -411286,7 +411808,7 @@ class FlexibleChatTransport {
|
|
|
411286
411808
|
tools,
|
|
411287
411809
|
maxRetries: 0,
|
|
411288
411810
|
onError: () => {},
|
|
411289
|
-
experimental_repairToolCall: makeRepairToolCall(model2),
|
|
411811
|
+
experimental_repairToolCall: makeRepairToolCall(chatId, model2),
|
|
411290
411812
|
experimental_download: async (items) => {
|
|
411291
411813
|
const promises5 = items.map(async ({
|
|
411292
411814
|
url: url3,
|
|
@@ -411366,7 +411888,7 @@ function estimateTotalTokens(messages2) {
|
|
|
411366
411888
|
}
|
|
411367
411889
|
|
|
411368
411890
|
// ../livekit/src/chat/live-chat-kit.ts
|
|
411369
|
-
var
|
|
411891
|
+
var logger23 = getLogger("LiveChatKit");
|
|
411370
411892
|
|
|
411371
411893
|
class LiveChatKit {
|
|
411372
411894
|
taskId;
|
|
@@ -411416,15 +411938,16 @@ class LiveChatKit {
|
|
|
411416
411938
|
const lastMessage = messages2.at(-1);
|
|
411417
411939
|
if (lastMessage?.role === "user" && lastMessage.metadata?.kind === "user" && lastMessage.metadata.compact) {
|
|
411418
411940
|
try {
|
|
411419
|
-
const model2 = createModel2({
|
|
411941
|
+
const model2 = createModel2({ llm: getters.getLLM() });
|
|
411420
411942
|
await compactTask({
|
|
411943
|
+
taskId: this.taskId,
|
|
411421
411944
|
model: model2,
|
|
411422
411945
|
messages: messages2,
|
|
411423
411946
|
abortSignal: abortSignal2,
|
|
411424
411947
|
inline: true
|
|
411425
411948
|
});
|
|
411426
411949
|
} catch (err2) {
|
|
411427
|
-
|
|
411950
|
+
logger23.error("Failed to compact task", err2);
|
|
411428
411951
|
throw err2;
|
|
411429
411952
|
}
|
|
411430
411953
|
}
|
|
@@ -411435,8 +411958,9 @@ class LiveChatKit {
|
|
|
411435
411958
|
this.spawn = async () => {
|
|
411436
411959
|
const taskId2 = crypto.randomUUID();
|
|
411437
411960
|
const { messages: messages2 } = this.chat;
|
|
411438
|
-
const model2 = createModel2({
|
|
411961
|
+
const model2 = createModel2({ llm: getters.getLLM() });
|
|
411439
411962
|
const summary6 = await compactTask({
|
|
411963
|
+
taskId: taskId2,
|
|
411440
411964
|
model: model2,
|
|
411441
411965
|
messages: messages2,
|
|
411442
411966
|
abortSignal
|
|
@@ -411518,7 +412042,7 @@ class LiveChatKit {
|
|
|
411518
412042
|
if (!task) {
|
|
411519
412043
|
throw new Error("Task not found");
|
|
411520
412044
|
}
|
|
411521
|
-
const getModel = () => createModel2({
|
|
412045
|
+
const getModel = () => createModel2({ llm: getters.getLLM() });
|
|
411522
412046
|
scheduleGenerateTitleJob({
|
|
411523
412047
|
taskId: this.taskId,
|
|
411524
412048
|
store,
|
|
@@ -411563,7 +412087,7 @@ class LiveChatKit {
|
|
|
411563
412087
|
}));
|
|
411564
412088
|
};
|
|
411565
412089
|
onError = (error46) => {
|
|
411566
|
-
|
|
412090
|
+
logger23.error("onError", error46);
|
|
411567
412091
|
const lastMessage = this.chat.messages.at(-1) || null;
|
|
411568
412092
|
this.store.commit(events.chatStreamFailed({
|
|
411569
412093
|
id: this.taskId,
|
|
@@ -411716,7 +412240,7 @@ import * as nodePath from "node:path";
|
|
|
411716
412240
|
|
|
411717
412241
|
// ../common/src/diff-utils.ts
|
|
411718
412242
|
var import_fast_levenshtein = __toESM(require_levenshtein(), 1);
|
|
411719
|
-
var
|
|
412243
|
+
var logger24 = getLogger("diffUtils");
|
|
411720
412244
|
function normalize6(content3) {
|
|
411721
412245
|
return content3.replace(/\r\n/g, `
|
|
411722
412246
|
`).trimEnd();
|
|
@@ -411729,7 +412253,7 @@ class DiffError extends Error {
|
|
|
411729
412253
|
}
|
|
411730
412254
|
}
|
|
411731
412255
|
async function parseDiffAndApply(fileContent3, searchContent, replaceContent, expectedReplacements = 1) {
|
|
411732
|
-
|
|
412256
|
+
logger24.trace(`Applying diff with expectedReplacements: ${expectedReplacements}`);
|
|
411733
412257
|
const isCRLF = fileContent3.includes(`\r
|
|
411734
412258
|
`);
|
|
411735
412259
|
const normalizedFileContent = normalize6(fileContent3);
|
|
@@ -411746,11 +412270,11 @@ async function parseDiffAndApply(fileContent3, searchContent, replaceContent, ex
|
|
|
411746
412270
|
const matches2 = searchContentExact(normalizedFileContent, normalizedSearchContent);
|
|
411747
412271
|
if (matches2.length < expectedReplacements) {
|
|
411748
412272
|
matches2.push(...searchContentWithLineTrimmed(normalizedFileContent, normalizedSearchContent));
|
|
411749
|
-
|
|
412273
|
+
logger24.trace(`Found ${matches2.length} matches after line trimming search strategy`);
|
|
411750
412274
|
}
|
|
411751
412275
|
if (matches2.length < expectedReplacements) {
|
|
411752
412276
|
matches2.push(...searchContentByBlockAnchor(normalizedFileContent, normalizedSearchContent));
|
|
411753
|
-
|
|
412277
|
+
logger24.trace(`Found ${matches2.length} matches after block anchor search strategy`);
|
|
411754
412278
|
}
|
|
411755
412279
|
if (matches2.length === 0) {
|
|
411756
412280
|
throw new DiffError("Search content does not match the file content. Try to reread the file for the latest content.");
|
|
@@ -411759,7 +412283,7 @@ async function parseDiffAndApply(fileContent3, searchContent, replaceContent, ex
|
|
|
411759
412283
|
throw new DiffError(`Expected ${expectedReplacements} occurrences but found ${matches2.length}. Please verify the search content and expectedReplacements parameter.`);
|
|
411760
412284
|
}
|
|
411761
412285
|
const result2 = replaceMatches(normalizedFileContent, matches2, replaceContent);
|
|
411762
|
-
|
|
412286
|
+
logger24.trace("Successfully applied diff");
|
|
411763
412287
|
if (isCRLF) {
|
|
411764
412288
|
return result2.replace(/\n/g, `\r
|
|
411765
412289
|
`);
|
|
@@ -412113,11 +412637,11 @@ var readFile13 = () => async ({ path: path27, startLine, endLine }, { cwd: cwd2
|
|
|
412113
412637
|
|
|
412114
412638
|
// src/tools/search-files.ts
|
|
412115
412639
|
import * as fs14 from "node:fs";
|
|
412116
|
-
var
|
|
412640
|
+
var logger25 = getLogger("searchFiles");
|
|
412117
412641
|
var searchFiles2 = (context15) => async ({ path: path27, regex: regex3, filePattern }, { abortSignal, cwd: cwd2 }) => {
|
|
412118
412642
|
const rgPath = context15.rg;
|
|
412119
412643
|
if (!rgPath || !fs14.existsSync(rgPath)) {
|
|
412120
|
-
|
|
412644
|
+
logger25.error("Ripgrep not found at path", rgPath);
|
|
412121
412645
|
throw new Error(`Ripgrep not found at path: ${rgPath}`);
|
|
412122
412646
|
}
|
|
412123
412647
|
return await searchFilesWithRipgrep(path27, regex3, rgPath, cwd2, filePattern, abortSignal);
|
|
@@ -412195,7 +412719,7 @@ async function executeToolCall(tool2, options6, cwd2, abortSignal) {
|
|
|
412195
412719
|
}
|
|
412196
412720
|
|
|
412197
412721
|
// src/task-runner.ts
|
|
412198
|
-
var
|
|
412722
|
+
var logger26 = getLogger("TaskRunner");
|
|
412199
412723
|
|
|
412200
412724
|
class TaskRunner {
|
|
412201
412725
|
cwd;
|
|
@@ -412272,9 +412796,9 @@ class TaskRunner {
|
|
|
412272
412796
|
return this.chatKit.task?.shareId;
|
|
412273
412797
|
}
|
|
412274
412798
|
async run() {
|
|
412275
|
-
|
|
412799
|
+
logger26.debug("Starting TaskRunner...");
|
|
412276
412800
|
try {
|
|
412277
|
-
|
|
412801
|
+
logger26.trace("Start step loop.");
|
|
412278
412802
|
this.stepCount.reset();
|
|
412279
412803
|
while (true) {
|
|
412280
412804
|
const stepResult = await this.step();
|
|
@@ -412289,7 +412813,7 @@ class TaskRunner {
|
|
|
412289
412813
|
}
|
|
412290
412814
|
} catch (e11) {
|
|
412291
412815
|
const error46 = toError2(e11);
|
|
412292
|
-
|
|
412816
|
+
logger26.trace("Failed:", error46);
|
|
412293
412817
|
}
|
|
412294
412818
|
}
|
|
412295
412819
|
async step() {
|
|
@@ -412327,24 +412851,24 @@ class TaskRunner {
|
|
|
412327
412851
|
throw new Error("Task is not loaded");
|
|
412328
412852
|
}
|
|
412329
412853
|
if ((task.status === "completed" || task.status === "pending-input") && isResultMessage(message)) {
|
|
412330
|
-
|
|
412854
|
+
logger26.trace("Task is completed or pending input, no more steps to process.");
|
|
412331
412855
|
return "finished";
|
|
412332
412856
|
}
|
|
412333
412857
|
if (task.status === "failed") {
|
|
412334
412858
|
if (task.error?.kind === "APICallError" && !task.error.isRetryable) {
|
|
412335
412859
|
return "finished";
|
|
412336
412860
|
}
|
|
412337
|
-
|
|
412861
|
+
logger26.error("Task is failed, trying to resend last message to resume it.", task.error);
|
|
412338
412862
|
return "retry";
|
|
412339
412863
|
}
|
|
412340
412864
|
if (message.role !== "assistant") {
|
|
412341
|
-
|
|
412865
|
+
logger26.trace("Last message is not a assistant message, resending it to resume the task.");
|
|
412342
412866
|
return "retry";
|
|
412343
412867
|
}
|
|
412344
412868
|
if (isAssistantMessageWithEmptyParts(message) || isAssistantMessageWithPartialToolCalls(message) || isAssistantMessageWithOutputError(message) || lastAssistantMessageIsCompleteWithToolCalls({
|
|
412345
412869
|
messages: this.chat.messages
|
|
412346
412870
|
})) {
|
|
412347
|
-
|
|
412871
|
+
logger26.trace("Last message is assistant with empty parts or partial/completed tool calls, resending it to resume the task.");
|
|
412348
412872
|
const processed = prepareLastMessageForRetry(message);
|
|
412349
412873
|
if (processed) {
|
|
412350
412874
|
this.chat.appendOrReplaceMessage(processed);
|
|
@@ -412352,28 +412876,28 @@ class TaskRunner {
|
|
|
412352
412876
|
return "retry";
|
|
412353
412877
|
}
|
|
412354
412878
|
if (isAssistantMessageWithNoToolCalls(message)) {
|
|
412355
|
-
|
|
412879
|
+
logger26.trace("Last message is assistant with no tool calls, sending a new user reminder.");
|
|
412356
412880
|
const message2 = createUserMessage(prompts.createSystemReminder("You should use tool calls to answer the question, for example, use attemptCompletion if the job is done, or use askFollowupQuestions to clarify the request."));
|
|
412357
412881
|
this.chat.appendOrReplaceMessage(message2);
|
|
412358
412882
|
return "retry";
|
|
412359
412883
|
}
|
|
412360
412884
|
}
|
|
412361
412885
|
async processToolCalls(message) {
|
|
412362
|
-
|
|
412886
|
+
logger26.trace("Processing tool calls in the last message.");
|
|
412363
412887
|
for (const toolCall of message.parts.filter(isToolUIPart)) {
|
|
412364
412888
|
if (toolCall.state !== "input-available")
|
|
412365
412889
|
continue;
|
|
412366
412890
|
const toolName = getToolName(toolCall);
|
|
412367
|
-
|
|
412891
|
+
logger26.trace(`Found tool call: ${toolName} with args: ${JSON.stringify(toolCall.input)}`);
|
|
412368
412892
|
const toolResult = await executeToolCall(toolCall, this.toolCallOptions, this.cwd);
|
|
412369
412893
|
await this.chatKit.chat.addToolResult({
|
|
412370
412894
|
tool: toolName,
|
|
412371
412895
|
toolCallId: toolCall.toolCallId,
|
|
412372
412896
|
output: toolResult
|
|
412373
412897
|
});
|
|
412374
|
-
|
|
412898
|
+
logger26.trace(`Tool call result: ${JSON.stringify(toolResult)}`);
|
|
412375
412899
|
}
|
|
412376
|
-
|
|
412900
|
+
logger26.trace("All tool calls processed in the last message.");
|
|
412377
412901
|
return "next";
|
|
412378
412902
|
}
|
|
412379
412903
|
}
|
|
@@ -412674,8 +413198,8 @@ function registerUpgradeCommand(program5) {
|
|
|
412674
413198
|
});
|
|
412675
413199
|
}
|
|
412676
413200
|
// src/cli.ts
|
|
412677
|
-
var
|
|
412678
|
-
|
|
413201
|
+
var logger27 = getLogger("Pochi");
|
|
413202
|
+
logger27.debug(`pochi v${package_default.version}`);
|
|
412679
413203
|
var parsePositiveInt = (input2) => {
|
|
412680
413204
|
if (!input2) {
|
|
412681
413205
|
return program5.error("The value for this option must be a positive integer.");
|
|
@@ -412798,8 +413322,7 @@ async function createLLMConfigWithVendors(program6, model2) {
|
|
|
412798
413322
|
return {
|
|
412799
413323
|
type: "vendor",
|
|
412800
413324
|
useToolCallMiddleware: options6.useToolCallMiddleware,
|
|
412801
|
-
getModel: (
|
|
412802
|
-
id: id5,
|
|
413325
|
+
getModel: () => createModel(vendorId, {
|
|
412803
413326
|
modelId,
|
|
412804
413327
|
getCredentials: vendor2.getCredentials
|
|
412805
413328
|
})
|
|
@@ -412815,8 +413338,7 @@ async function createLLMConfigWithPochi(model2) {
|
|
|
412815
413338
|
return {
|
|
412816
413339
|
type: "vendor",
|
|
412817
413340
|
useToolCallMiddleware: pochiModelOptions.useToolCallMiddleware,
|
|
412818
|
-
getModel: (
|
|
412819
|
-
id: id5,
|
|
413341
|
+
getModel: () => createModel(vendorId, {
|
|
412820
413342
|
modelId: model2,
|
|
412821
413343
|
getCredentials: vendor2.getCredentials
|
|
412822
413344
|
})
|