@astrive-ai/providers 1.0.11 → 1.0.13
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"overchat-provider.d.ts","sourceRoot":"","sources":["../src/overchat-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGjG,qBAAa,gBAAiB,SAAQ,YAAY;IAChD,OAAO,CAAC,GAAG,CAAiD;IAC5D,OAAO,CAAC,EAAE,CAAqH;IAC/H,OAAO,CAAC,MAAM,CAAiC;gBAEnC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa;IAIlC,WAAW,IAAI,OAAO;IAItC,OAAO,CAAC,oBAAoB;IAmB5B,OAAO,CAAC,WAAW;
|
|
1
|
+
{"version":3,"file":"overchat-provider.d.ts","sourceRoot":"","sources":["../src/overchat-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGjG,qBAAa,gBAAiB,SAAQ,YAAY;IAChD,OAAO,CAAC,GAAG,CAAiD;IAC5D,OAAO,CAAC,EAAE,CAAqH;IAC/H,OAAO,CAAC,MAAM,CAAiC;gBAEnC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa;IAIlC,WAAW,IAAI,OAAO;IAItC,OAAO,CAAC,oBAAoB;IAmB5B,OAAO,CAAC,WAAW;IAsCN,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IA4ChD,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC;IA6EjF,SAAS,IAAI,MAAM,EAAE;IAIrB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;CAIjD"}
|
|
@@ -29,62 +29,79 @@ export class OverchatProvider extends BaseProvider {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
mapMessages(request) {
|
|
32
|
-
|
|
32
|
+
const messages = request.messages.map(msg => {
|
|
33
33
|
const mapped = { ...msg };
|
|
34
34
|
if (!mapped.id)
|
|
35
35
|
mapped.id = crypto.randomUUID();
|
|
36
|
-
if (mapped.
|
|
37
|
-
mapped.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
if (mapped.role === 'tool') {
|
|
37
|
+
mapped.role = 'user';
|
|
38
|
+
mapped.content = `[Tool Result for ${mapped.name}]: ${mapped.content}`;
|
|
39
|
+
}
|
|
40
|
+
else if (mapped.role === 'assistant' && mapped.tool_calls) {
|
|
41
|
+
mapped.content = mapped.content || '';
|
|
42
|
+
mapped.content += `\n\`\`\`json\n{"tool_calls":${JSON.stringify(mapped.tool_calls.map((tc) => ({
|
|
43
|
+
name: tc.function?.name || tc.name,
|
|
44
|
+
arguments: tc.function?.arguments || tc.arguments
|
|
45
|
+
})))}}\n\`\`\``;
|
|
46
|
+
delete mapped.tool_calls;
|
|
45
47
|
}
|
|
46
48
|
return mapped;
|
|
47
49
|
});
|
|
50
|
+
if (request.tools && request.tools.length > 0) {
|
|
51
|
+
const toolDefs = JSON.stringify(request.tools.map(t => ({
|
|
52
|
+
name: t.function.name,
|
|
53
|
+
description: t.function.description,
|
|
54
|
+
parameters: t.function.parameters
|
|
55
|
+
})));
|
|
56
|
+
const instruction = `\n\n[CRITICAL INSTRUCTION] You have access to the following tools: ${toolDefs}. If you need to use a tool to fulfill the user's request (like executing a terminal command, downloading, etc), you MUST reply with a JSON code block in EXACTLY this format and NO OTHER TEXT:\n\`\`\`json\n{"tool_calls":[{"name":"tool_name","arguments":{"arg1":"value1"}}]}\n\`\`\`\nDo not provide any explanation if you are calling a tool, ONLY the JSON block.`;
|
|
57
|
+
const systemMsg = messages.find(m => m.role === 'system');
|
|
58
|
+
if (systemMsg) {
|
|
59
|
+
systemMsg.content += instruction;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
messages.unshift({ id: crypto.randomUUID(), role: 'system', content: instruction });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return messages;
|
|
48
66
|
}
|
|
49
67
|
async chat(request) {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
stream: false,
|
|
61
|
-
temperature: request.temperature || 0.5,
|
|
62
|
-
top_p: 0.95,
|
|
63
|
-
};
|
|
64
|
-
if (request.tools && request.tools.length > 0) {
|
|
65
|
-
body.tools = request.tools;
|
|
68
|
+
const stream = this.chatStream(request);
|
|
69
|
+
let fullContent = '';
|
|
70
|
+
let toolCalls = [];
|
|
71
|
+
for await (const chunk of stream) {
|
|
72
|
+
if (chunk.content) {
|
|
73
|
+
fullContent += chunk.content;
|
|
74
|
+
}
|
|
75
|
+
if (chunk.toolCalls && chunk.toolCalls.length > 0) {
|
|
76
|
+
toolCalls = chunk.toolCalls;
|
|
77
|
+
}
|
|
66
78
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
let finalContent = fullContent;
|
|
80
|
+
let match = fullContent.match(/```(?:json)?\s*(\{[\s\S]*?"tool_calls"[\s\S]*?\})\s*```/);
|
|
81
|
+
if (!match) {
|
|
82
|
+
match = fullContent.match(/(\{[\s\S]*?"tool_calls"[\s\S]*?\})/);
|
|
83
|
+
}
|
|
84
|
+
if (match) {
|
|
85
|
+
try {
|
|
86
|
+
const parsed = JSON.parse(match[1]);
|
|
87
|
+
if (parsed.tool_calls && Array.isArray(parsed.tool_calls)) {
|
|
88
|
+
toolCalls = parsed.tool_calls.map((tc) => ({
|
|
89
|
+
id: `call_${crypto.randomUUID()}`,
|
|
90
|
+
name: tc.name,
|
|
91
|
+
arguments: typeof tc.arguments === 'string' ? JSON.parse(tc.arguments) : (tc.arguments || tc.parameters || {})
|
|
92
|
+
}));
|
|
93
|
+
finalContent = fullContent.replace(match[0], '').trim();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (e) { }
|
|
75
97
|
}
|
|
76
|
-
const data = await response.json();
|
|
77
98
|
return {
|
|
78
|
-
id:
|
|
79
|
-
content:
|
|
99
|
+
id: crypto.randomUUID(),
|
|
100
|
+
content: finalContent,
|
|
80
101
|
role: 'assistant',
|
|
81
|
-
toolCalls:
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
arguments: typeof tc.function.arguments === 'string' ? JSON.parse(tc.function.arguments) : tc.function.arguments
|
|
85
|
-
})),
|
|
86
|
-
model: data.model || this.models[0],
|
|
87
|
-
finishReason: data.choices?.[0]?.finish_reason || 'stop'
|
|
102
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
103
|
+
model: this.models[0],
|
|
104
|
+
finishReason: 'stop'
|
|
88
105
|
};
|
|
89
106
|
}
|
|
90
107
|
async *chatStream(request) {
|
|
@@ -102,9 +119,7 @@ export class OverchatProvider extends BaseProvider {
|
|
|
102
119
|
temperature: request.temperature || 0.5,
|
|
103
120
|
top_p: 0.95,
|
|
104
121
|
};
|
|
105
|
-
|
|
106
|
-
body.tools = request.tools;
|
|
107
|
-
}
|
|
122
|
+
// Removed body.tools since it's injected via system prompt now
|
|
108
123
|
const response = await fetch(this.API, {
|
|
109
124
|
method: "POST",
|
|
110
125
|
headers: this.buildOverchatHeaders(deviceId),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"overchat-provider.js","sourceRoot":"","sources":["../src/overchat-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,MAAM,OAAO,gBAAiB,SAAQ,YAAY;IACxC,GAAG,GAAG,6CAA6C,CAAC;IACpD,EAAE,GAAG,iHAAiH,CAAC;IACvH,MAAM,GAAG,CAAC,2BAA2B,CAAC,CAAC;IAE/C,YAAY,MAAe,EAAE,MAAqB;QAChD,KAAK,CAAC,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACjD,CAAC;IAEe,WAAW;QACzB,OAAO,IAAI,CAAC,CAAC,sBAAsB;IACrC,CAAC;IAEO,oBAAoB,CAAC,QAAgB;QAC3C,OAAO;YACL,oBAAoB,EAAE,WAAW;YACjC,eAAe,EAAE,QAAQ;YACzB,WAAW,EAAE,kEAAkE;YAC/E,kBAAkB,EAAE,IAAI;YACxB,mBAAmB,EAAE,OAAO;YAC5B,mBAAmB,EAAE,KAAK;YAC1B,kBAAkB,EAAE,QAAQ;YAC5B,YAAY,EAAE,IAAI,CAAC,EAAE;YACrB,MAAM,EAAE,KAAK;YACb,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,qBAAqB;YAC7B,OAAO,EAAE,sBAAsB;YAC/B,iBAAiB,EAAE,gBAAgB;YACnC,QAAQ,EAAE,QAAQ;SACnB,CAAC;IACJ,CAAC;IAEO,WAAW,CAAC,OAAoB;QACtC,
|
|
1
|
+
{"version":3,"file":"overchat-provider.js","sourceRoot":"","sources":["../src/overchat-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,MAAM,OAAO,gBAAiB,SAAQ,YAAY;IACxC,GAAG,GAAG,6CAA6C,CAAC;IACpD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,SAAS,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAC5D,OAAO,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;CACF"}
|
package/package.json
CHANGED
package/src/overchat-provider.ts
CHANGED
|
@@ -35,67 +35,84 @@ export class OverchatProvider extends BaseProvider {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
private mapMessages(request: ChatRequest) {
|
|
38
|
-
|
|
38
|
+
const messages = request.messages.map(msg => {
|
|
39
39
|
const mapped = { ...msg } as any;
|
|
40
40
|
if (!mapped.id) mapped.id = crypto.randomUUID();
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
|
|
42
|
+
if (mapped.role === 'tool') {
|
|
43
|
+
mapped.role = 'user';
|
|
44
|
+
mapped.content = `[Tool Result for ${mapped.name}]: ${mapped.content}`;
|
|
45
|
+
} else if (mapped.role === 'assistant' && mapped.tool_calls) {
|
|
46
|
+
mapped.content = mapped.content || '';
|
|
47
|
+
mapped.content += `\n\`\`\`json\n{"tool_calls":${JSON.stringify(mapped.tool_calls.map((tc: any) => ({
|
|
48
|
+
name: tc.function?.name || tc.name,
|
|
49
|
+
arguments: tc.function?.arguments || tc.arguments
|
|
50
|
+
})))}}\n\`\`\``;
|
|
51
|
+
delete mapped.tool_calls;
|
|
50
52
|
}
|
|
51
53
|
return mapped;
|
|
52
54
|
});
|
|
55
|
+
|
|
56
|
+
if (request.tools && request.tools.length > 0) {
|
|
57
|
+
const toolDefs = JSON.stringify(request.tools.map(t => ({
|
|
58
|
+
name: t.function.name,
|
|
59
|
+
description: t.function.description,
|
|
60
|
+
parameters: t.function.parameters
|
|
61
|
+
})));
|
|
62
|
+
const instruction = `\n\n[CRITICAL INSTRUCTION] You have access to the following tools: ${toolDefs}. If you need to use a tool to fulfill the user's request (like executing a terminal command, downloading, etc), you MUST reply with a JSON code block in EXACTLY this format and NO OTHER TEXT:\n\`\`\`json\n{"tool_calls":[{"name":"tool_name","arguments":{"arg1":"value1"}}]}\n\`\`\`\nDo not provide any explanation if you are calling a tool, ONLY the JSON block.`;
|
|
63
|
+
|
|
64
|
+
const systemMsg = messages.find(m => m.role === 'system');
|
|
65
|
+
if (systemMsg) {
|
|
66
|
+
systemMsg.content += instruction;
|
|
67
|
+
} else {
|
|
68
|
+
messages.unshift({ id: crypto.randomUUID(), role: 'system', content: instruction });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return messages;
|
|
53
73
|
}
|
|
54
74
|
|
|
55
75
|
public async chat(request: ChatRequest): Promise<ChatResponse> {
|
|
56
|
-
const
|
|
57
|
-
|
|
76
|
+
const stream = this.chatStream(request);
|
|
77
|
+
let fullContent = '';
|
|
78
|
+
let toolCalls: any[] = [];
|
|
58
79
|
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
presence_penalty: 0,
|
|
67
|
-
stream: false,
|
|
68
|
-
temperature: request.temperature || 0.5,
|
|
69
|
-
top_p: 0.95,
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
if (request.tools && request.tools.length > 0) {
|
|
73
|
-
body.tools = request.tools;
|
|
80
|
+
for await (const chunk of stream) {
|
|
81
|
+
if (chunk.content) {
|
|
82
|
+
fullContent += chunk.content;
|
|
83
|
+
}
|
|
84
|
+
if (chunk.toolCalls && chunk.toolCalls.length > 0) {
|
|
85
|
+
toolCalls = chunk.toolCalls;
|
|
86
|
+
}
|
|
74
87
|
}
|
|
75
88
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
89
|
+
let finalContent = fullContent;
|
|
90
|
+
let match = fullContent.match(/```(?:json)?\s*(\{[\s\S]*?"tool_calls"[\s\S]*?\})\s*```/);
|
|
91
|
+
if (!match) {
|
|
92
|
+
match = fullContent.match(/(\{[\s\S]*?"tool_calls"[\s\S]*?\})/);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (match) {
|
|
96
|
+
try {
|
|
97
|
+
const parsed = JSON.parse(match[1]);
|
|
98
|
+
if (parsed.tool_calls && Array.isArray(parsed.tool_calls)) {
|
|
99
|
+
toolCalls = parsed.tool_calls.map((tc: any) => ({
|
|
100
|
+
id: `call_${crypto.randomUUID()}`,
|
|
101
|
+
name: tc.name,
|
|
102
|
+
arguments: typeof tc.arguments === 'string' ? JSON.parse(tc.arguments) : (tc.arguments || tc.parameters || {})
|
|
103
|
+
}));
|
|
104
|
+
finalContent = fullContent.replace(match[0], '').trim();
|
|
105
|
+
}
|
|
106
|
+
} catch (e) {}
|
|
85
107
|
}
|
|
86
108
|
|
|
87
|
-
const data: any = await response.json();
|
|
88
109
|
return {
|
|
89
|
-
id:
|
|
90
|
-
content:
|
|
110
|
+
id: crypto.randomUUID(),
|
|
111
|
+
content: finalContent,
|
|
91
112
|
role: 'assistant',
|
|
92
|
-
toolCalls:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
arguments: typeof tc.function.arguments === 'string' ? JSON.parse(tc.function.arguments) : tc.function.arguments
|
|
96
|
-
})),
|
|
97
|
-
model: data.model || this.models[0],
|
|
98
|
-
finishReason: data.choices?.[0]?.finish_reason || 'stop'
|
|
113
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
114
|
+
model: this.models[0],
|
|
115
|
+
finishReason: 'stop'
|
|
99
116
|
};
|
|
100
117
|
}
|
|
101
118
|
|
|
@@ -116,9 +133,7 @@ export class OverchatProvider extends BaseProvider {
|
|
|
116
133
|
top_p: 0.95,
|
|
117
134
|
};
|
|
118
135
|
|
|
119
|
-
|
|
120
|
-
body.tools = request.tools;
|
|
121
|
-
}
|
|
136
|
+
// Removed body.tools since it's injected via system prompt now
|
|
122
137
|
|
|
123
138
|
const response = await fetch(this.API, {
|
|
124
139
|
method: "POST",
|