devin-engine 0.0.0-stage → 0.0.1
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/README.md +47 -2
- package/dist/engine.d.ts +55 -0
- package/dist/engine.js +543 -0
- package/dist/frames.d.ts +29 -0
- package/dist/frames.js +276 -0
- package/dist/guard.d.ts +32 -0
- package/dist/guard.js +114 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +15 -0
- package/dist/override.d.ts +25 -0
- package/dist/override.js +78 -0
- package/dist/reasoning.d.ts +20 -0
- package/dist/reasoning.js +60 -0
- package/dist/resolve.d.ts +29 -0
- package/dist/resolve.js +86 -0
- package/dist/sanitize.d.ts +26 -0
- package/dist/sanitize.js +144 -0
- package/dist/tools.d.ts +39 -0
- package/dist/tools.js +245 -0
- package/dist/translate.d.ts +41 -0
- package/dist/translate.js +91 -0
- package/package.json +47 -5
- package/prompts/override-compact-es.txt +17 -0
- package/prompts/override-compact.txt +17 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-facing model-id resolution — OpenAI id + reasoning_effort -> wire uid.
|
|
3
|
+
*
|
|
4
|
+
* Ported from devin_proxy/models/catalog.py (the resolve_model half; the
|
|
5
|
+
* catalog parse lives in devin-core).
|
|
6
|
+
*/
|
|
7
|
+
/** Wire uids for the base SWE-2 lanes. */
|
|
8
|
+
export declare const SWE2_TIERS: readonly ["swe-2-medium", "swe-2-high", "swe-2-max"];
|
|
9
|
+
/** SWE-2 Flash lanes — the fast strawberry-pancake family. */
|
|
10
|
+
export declare const SWE2_FLASH_TIERS: readonly ["swe-2-flash-medium", "swe-2-flash-high", "swe-2-flash-max"];
|
|
11
|
+
/** SWE-2.1 lanes — five effort levels, no flash. */
|
|
12
|
+
export declare const SWE21_TIERS: readonly ["swe-2-1-low", "swe-2-1-medium", "swe-2-1-high", "swe-2-1-xhigh", "swe-2-1-max"];
|
|
13
|
+
/** Every lane we know how to route, in stable order. Lightning excluded — bills fast-mode credits. */
|
|
14
|
+
export declare const ALL_TIERS: readonly ["swe-2-medium", "swe-2-high", "swe-2-max", "swe-2-flash-medium", "swe-2-flash-high", "swe-2-flash-max", "swe-2-1-low", "swe-2-1-medium", "swe-2-1-high", "swe-2-1-xhigh", "swe-2-1-max"];
|
|
15
|
+
export interface ResolveOptions {
|
|
16
|
+
effort?: string;
|
|
17
|
+
allowed?: readonly string[];
|
|
18
|
+
default?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Map an OpenAI model id + reasoning_effort to a wire uid.
|
|
22
|
+
*
|
|
23
|
+
* Anything outside `allowed` falls back to `default`. A bare "flash" /
|
|
24
|
+
* "swe-2-flash" id lands on the flash-high lane; "swe-2-1"/"swe-2.1"
|
|
25
|
+
* selects the 2.1 family. Effort picks a tier inside whatever family
|
|
26
|
+
* the model id selected (default: the base family), clamping to the
|
|
27
|
+
* nearest defined level when the family lacks that effort name.
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveModel(model: string, opts?: ResolveOptions): string;
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-facing model-id resolution — OpenAI id + reasoning_effort -> wire uid.
|
|
3
|
+
*
|
|
4
|
+
* Ported from devin_proxy/models/catalog.py (the resolve_model half; the
|
|
5
|
+
* catalog parse lives in devin-core).
|
|
6
|
+
*/
|
|
7
|
+
/** Wire uids for the base SWE-2 lanes. */
|
|
8
|
+
export const SWE2_TIERS = ["swe-2-medium", "swe-2-high", "swe-2-max"];
|
|
9
|
+
/** SWE-2 Flash lanes — the fast strawberry-pancake family. */
|
|
10
|
+
export const SWE2_FLASH_TIERS = ["swe-2-flash-medium", "swe-2-flash-high", "swe-2-flash-max"];
|
|
11
|
+
/** SWE-2.1 lanes — five effort levels, no flash. */
|
|
12
|
+
export const SWE21_TIERS = [
|
|
13
|
+
"swe-2-1-low",
|
|
14
|
+
"swe-2-1-medium",
|
|
15
|
+
"swe-2-1-high",
|
|
16
|
+
"swe-2-1-xhigh",
|
|
17
|
+
"swe-2-1-max",
|
|
18
|
+
];
|
|
19
|
+
/** Every lane we know how to route, in stable order. Lightning excluded — bills fast-mode credits. */
|
|
20
|
+
export const ALL_TIERS = [...SWE2_TIERS, ...SWE2_FLASH_TIERS, ...SWE21_TIERS];
|
|
21
|
+
/** Effort word -> index inside a family tuple (cheapest->strongest). */
|
|
22
|
+
const EFFORT_RANK = {
|
|
23
|
+
off: 0,
|
|
24
|
+
none: 0,
|
|
25
|
+
minimal: 0,
|
|
26
|
+
low: 1,
|
|
27
|
+
medium: 2,
|
|
28
|
+
high: 3,
|
|
29
|
+
xhigh: 4,
|
|
30
|
+
max: 5,
|
|
31
|
+
};
|
|
32
|
+
/** (family tiers, effort-name tuple) for a model id. */
|
|
33
|
+
function familyTiers(m) {
|
|
34
|
+
if (m.includes("flash")) {
|
|
35
|
+
return { tiers: SWE2_FLASH_TIERS, names: ["medium", "high", "max"] };
|
|
36
|
+
}
|
|
37
|
+
if (m.includes("2-1") || m.includes("2.1") || m.includes("2p1")) {
|
|
38
|
+
return { tiers: SWE21_TIERS, names: ["low", "medium", "high", "xhigh", "max"] };
|
|
39
|
+
}
|
|
40
|
+
return { tiers: SWE2_TIERS, names: ["medium", "high", "max"] };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Map an OpenAI model id + reasoning_effort to a wire uid.
|
|
44
|
+
*
|
|
45
|
+
* Anything outside `allowed` falls back to `default`. A bare "flash" /
|
|
46
|
+
* "swe-2-flash" id lands on the flash-high lane; "swe-2-1"/"swe-2.1"
|
|
47
|
+
* selects the 2.1 family. Effort picks a tier inside whatever family
|
|
48
|
+
* the model id selected (default: the base family), clamping to the
|
|
49
|
+
* nearest defined level when the family lacks that effort name.
|
|
50
|
+
*/
|
|
51
|
+
export function resolveModel(model, opts = {}) {
|
|
52
|
+
const { effort = "", allowed = ALL_TIERS, default: def = "swe-2-high" } = opts;
|
|
53
|
+
const m = (model ?? "").trim().toLowerCase();
|
|
54
|
+
const e = (effort ?? "").trim().toLowerCase();
|
|
55
|
+
// explicit lane always wins
|
|
56
|
+
for (const cand of allowed) {
|
|
57
|
+
if (m === cand || m === cand.replace("swe-2-", "swe2-") || m === cand.replace("swe-2-1-", "swe-2.1-")) {
|
|
58
|
+
return cand;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const { tiers, names } = familyTiers(m);
|
|
62
|
+
let tier;
|
|
63
|
+
if (e in EFFORT_RANK) {
|
|
64
|
+
const rank = EFFORT_RANK[e];
|
|
65
|
+
// nearest defined level by rank distance; ties resolve upward so an
|
|
66
|
+
// effort the family lacks lands on the stronger neighbor
|
|
67
|
+
let idx = 0;
|
|
68
|
+
let best = Infinity;
|
|
69
|
+
for (let i = 0; i < names.length; i++) {
|
|
70
|
+
const d = Math.abs(EFFORT_RANK[names[i]] - rank);
|
|
71
|
+
if (d < best || (d === best && EFFORT_RANK[names[i]] > EFFORT_RANK[names[idx]])) {
|
|
72
|
+
best = d;
|
|
73
|
+
idx = i;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
tier = tiers[idx];
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
tier = tiers.includes(def) ? def : tiers[Math.floor(tiers.length / 2)];
|
|
80
|
+
}
|
|
81
|
+
if (allowed.includes(tier))
|
|
82
|
+
return tier;
|
|
83
|
+
if (allowed.includes(def))
|
|
84
|
+
return def;
|
|
85
|
+
return allowed[0] ?? tier;
|
|
86
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History hygiene: drop empty assistant turns, strip transport notes and the
|
|
3
|
+
* model's own past refusals before they can anchor the next turn.
|
|
4
|
+
*
|
|
5
|
+
* Ported from devin_proxy/jailbreak/sanitize.py.
|
|
6
|
+
*/
|
|
7
|
+
export declare const MODEL_SWITCH_NOTE_RE: RegExp;
|
|
8
|
+
export declare const HISTORY_REFUSAL_RE: RegExp;
|
|
9
|
+
interface Message {
|
|
10
|
+
role?: string;
|
|
11
|
+
content?: string | Array<{
|
|
12
|
+
text?: string;
|
|
13
|
+
}>;
|
|
14
|
+
[k: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
/** Drop only truly empty assistant history entries. */
|
|
17
|
+
export declare function sanitizeMessages(msgs: Message[]): {
|
|
18
|
+
cleaned: Message[];
|
|
19
|
+
dropped: number;
|
|
20
|
+
};
|
|
21
|
+
/** Sentence-level excision of transport notes and past refusals. */
|
|
22
|
+
export declare function sanitizeHistoryArtifacts(msgs: Message[]): {
|
|
23
|
+
cleaned: Message[];
|
|
24
|
+
stats: Record<string, number>;
|
|
25
|
+
};
|
|
26
|
+
export {};
|
package/dist/sanitize.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History hygiene: drop empty assistant turns, strip transport notes and the
|
|
3
|
+
* model's own past refusals before they can anchor the next turn.
|
|
4
|
+
*
|
|
5
|
+
* Ported from devin_proxy/jailbreak/sanitize.py.
|
|
6
|
+
*/
|
|
7
|
+
/** NFC-normalize + collapse whitespace for stable regex matching. */
|
|
8
|
+
const norm = (text) => (typeof text === "string" ? text.normalize("NFC") : String(text ?? ""))
|
|
9
|
+
.replace(/\s+/g, " ")
|
|
10
|
+
.trim();
|
|
11
|
+
export const MODEL_SWITCH_NOTE_RE = /^\s*\[Note:\s*model was just switched\b.*?Adjust your self-identification accordingly\.\]\s*/is;
|
|
12
|
+
export const HISTORY_REFUSAL_RE = new RegExp("^\\s*(?:\u043d\u0435\u0442[.,!\u2026]|\u0441\u0442\u043e\u043f\\b|" +
|
|
13
|
+
"\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0441\u044c\\b|" +
|
|
14
|
+
"\u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\\b|" +
|
|
15
|
+
"\u043e\u0442\u0432\u0435\u0442 \u043e\u0441\u0442\u0430[\u0435\u0451]\u0442\u0441\u044f \u0442\u0435\u043c \u0436\u0435|" +
|
|
16
|
+
"\u0441 \u044d\u0442\u0438\u043c \u044f \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0435 \u043c\u043e\u0433\u0443|" +
|
|
17
|
+
"\u044f \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0443|" +
|
|
18
|
+
"\u0437\u0434\u0435\u0441\u044c \u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e\u0441\u044c|" +
|
|
19
|
+
"i (?:can't|cannot|won't)|i(?:'m| am) stopping)" +
|
|
20
|
+
"|\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e\u0441\u044c \u0438 \u0441\u043a\u0430\u0436\u0443|" +
|
|
21
|
+
"\u0434\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u043e\u0439\u0434\u0443|" +
|
|
22
|
+
"\u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u044d\u0442\u0443 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443|" +
|
|
23
|
+
"\u043f\u043e\u0437\u0438\u0446\u0438[\u0438\u044f] \u043d\u0435 \u0441\u0434\u0432\u0438\u043d\u0443\u043b|" +
|
|
24
|
+
"\u043e\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f \u043d\u0435 \u0441\u0434\u0432\u0438\u043d\u0435\u0442\u0441\u044f|" +
|
|
25
|
+
"\u0433\u0434\u0435 \u043c\u043e\u0439 \u043f\u0440\u0435\u0434\u0435\u043b|" +
|
|
26
|
+
"\u043d\u0435 \u0431\u0443\u0434\u0443 \u0434\u0435\u043b\u0430\u0442\u044c: \u043f\u0438\u0441\u0430\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u043f\u0443\u043b\u044b|" +
|
|
27
|
+
"\u043d\u0435 \u043c\u043e\u0433\u0443 \u043f\u043e\u043c\u043e\u0447\u044c|" +
|
|
28
|
+
"\u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442 \u043f\u043e\u043b\u0438\u0442\u0438\u043a|" +
|
|
29
|
+
"\u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u0430|" +
|
|
30
|
+
"\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0443|" +
|
|
31
|
+
"\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0443|" +
|
|
32
|
+
"\u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0443|" +
|
|
33
|
+
"\u043d\u0435 \u0431\u0443\u0434\u0443 \u2014|\u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0442\u043a\u0430\u0437|" +
|
|
34
|
+
"\u044f \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0441\u044c|" +
|
|
35
|
+
"\u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0441\u044c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432|" +
|
|
36
|
+
"no puedo (?:ayudar|seguir|continuar|escribir|proporcionar|colaborar)|" +
|
|
37
|
+
"no voy a (?:seguir|continuar|ejecutar|hacer|ayudar)|" +
|
|
38
|
+
"hasta aqu[i\u00ed] puedo|me niego a|no es algo que vaya a|" +
|
|
39
|
+
"acceso no autorizado a|lo siento,? (?:pero )?no puedo" +
|
|
40
|
+
"|\u62b1\u6b49[\u002c\uff0c]?\u6211(?:\u4e0d\u80fd|\u65e0\u6cd5|\u4e0d\u4f1a)|" +
|
|
41
|
+
"\u6211(?:\u4e0d\u80fd|\u65e0\u6cd5|\u4e0d\u4f1a)(?:\u5e2e\u52a9|\u534f\u52a9|\u63d0\u4f9b|\u5199|\u751f\u6210|\u7ee7\u7eed)|" +
|
|
42
|
+
"\u8fd9(?:\u4e2a|\u79cd)?(?:\u8bf7\u6c42|\u8981\u6c42|\u5185\u5bb9).{0,10}(?:\u8fdd\u53cd|\u4e0d\u5f53|\u654f\u611f|\u4e0d\u7b26\u5408)|" +
|
|
43
|
+
"\u4f5c\u4e3a(?:\u4e00\u4e2a)?(?:\u4eba\u5de5\u667a\u80fd|AI|\u8bed\u8a00\u6a21\u578b)|" +
|
|
44
|
+
"\u6211(?:\u53ea|\u4ec5)\u80fd(?:\u5e2e\u52a9|\u63d0\u4f9b|\u5efa\u8bae)|" +
|
|
45
|
+
"\u8c22\u7edd|\u4e0d\u4e88\u63d0\u4f9b", "i");
|
|
46
|
+
/** Drop only truly empty assistant history entries. */
|
|
47
|
+
export function sanitizeMessages(msgs) {
|
|
48
|
+
const cleaned = [];
|
|
49
|
+
let dropped = 0;
|
|
50
|
+
for (const m of msgs) {
|
|
51
|
+
if (!m || m.role !== "assistant") {
|
|
52
|
+
cleaned.push(m);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const content = m.content;
|
|
56
|
+
const hasContent = typeof content === "string"
|
|
57
|
+
? Boolean(content.trim())
|
|
58
|
+
: Array.isArray(content)
|
|
59
|
+
? content.length > 0
|
|
60
|
+
: content != null;
|
|
61
|
+
const hasOther = ["tool_calls", "function_call", "reasoning_content", "reasoning", "refusal", "audio"].some((k) => m[k]);
|
|
62
|
+
if (hasContent || hasOther)
|
|
63
|
+
cleaned.push(m);
|
|
64
|
+
else
|
|
65
|
+
dropped++;
|
|
66
|
+
}
|
|
67
|
+
return { cleaned, dropped };
|
|
68
|
+
}
|
|
69
|
+
/** Sentence-level excision of transport notes and past refusals. */
|
|
70
|
+
export function sanitizeHistoryArtifacts(msgs) {
|
|
71
|
+
const cleaned = [];
|
|
72
|
+
const stats = { switch_notes: 0, prefill_echoes: 0, history_refusals: 0 };
|
|
73
|
+
for (const original of msgs) {
|
|
74
|
+
if (!original || typeof original !== "object") {
|
|
75
|
+
cleaned.push(original);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const m = { ...original };
|
|
79
|
+
const role = m.role;
|
|
80
|
+
if (role === "user") {
|
|
81
|
+
const content = m.content;
|
|
82
|
+
if (typeof content === "string") {
|
|
83
|
+
const stripped = content.replace(MODEL_SWITCH_NOTE_RE, "");
|
|
84
|
+
if (stripped !== content) {
|
|
85
|
+
stats.switch_notes++;
|
|
86
|
+
m.content = stripped;
|
|
87
|
+
if (!stripped.trim())
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (Array.isArray(content)) {
|
|
92
|
+
let changed = false;
|
|
93
|
+
const parts = content.map((part) => {
|
|
94
|
+
if (!part || typeof part !== "object" || typeof part.text !== "string")
|
|
95
|
+
return part;
|
|
96
|
+
const stripped = part.text.replace(MODEL_SWITCH_NOTE_RE, "");
|
|
97
|
+
if (stripped !== part.text) {
|
|
98
|
+
stats.switch_notes++;
|
|
99
|
+
changed = true;
|
|
100
|
+
return { ...part, text: stripped };
|
|
101
|
+
}
|
|
102
|
+
return part;
|
|
103
|
+
});
|
|
104
|
+
if (changed)
|
|
105
|
+
m.content = parts;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (role === "assistant") {
|
|
109
|
+
const content = m.content;
|
|
110
|
+
if (typeof content === "string" &&
|
|
111
|
+
HISTORY_REFUSAL_RE.test(norm(content.slice(0, 4000))) &&
|
|
112
|
+
!m.tool_calls &&
|
|
113
|
+
!m.function_call) {
|
|
114
|
+
const originalContent = content;
|
|
115
|
+
// split keeping separators
|
|
116
|
+
const parts = content.split(/((?<=[.!?\u2026])\s+|(?<=[\u3002\uff01\uff1f\uff1b]))/);
|
|
117
|
+
const kept = [];
|
|
118
|
+
for (let off = 0; off < parts.length; off += 2) {
|
|
119
|
+
const sent = parts[off];
|
|
120
|
+
const sep = parts[off + 1] ?? "";
|
|
121
|
+
if (!sent)
|
|
122
|
+
continue;
|
|
123
|
+
if (!HISTORY_REFUSAL_RE.test(norm(sent.slice(0, 400))))
|
|
124
|
+
kept.push(sent + sep);
|
|
125
|
+
}
|
|
126
|
+
const newContent = kept.join("").trim();
|
|
127
|
+
if (newContent.length < 20 || newContent.length < originalContent.length * 0.3) {
|
|
128
|
+
stats.history_refusals++;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (newContent !== originalContent) {
|
|
132
|
+
stats.history_refusals++;
|
|
133
|
+
m.content = newContent;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
stats.history_refusals++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
cleaned.push(m);
|
|
142
|
+
}
|
|
143
|
+
return { cleaned, stats };
|
|
144
|
+
}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-calling support: native wire declarations (field 10) as the primary
|
|
3
|
+
* channel, the <<TOOL_CALL>> text protocol as the fallback, plus the parser
|
|
4
|
+
* that turns either back into OpenAI tool_calls.
|
|
5
|
+
*
|
|
6
|
+
* Ported from devin_proxy/jailbreak/tools.py.
|
|
7
|
+
*/
|
|
8
|
+
export declare const TOOL_OPEN = "<<TOOL_CALL>>";
|
|
9
|
+
export declare const TOOL_CLOSE = "<<END_TOOL_CALL>>";
|
|
10
|
+
export interface ToolCall {
|
|
11
|
+
id: string;
|
|
12
|
+
type: "function";
|
|
13
|
+
function: {
|
|
14
|
+
name: string;
|
|
15
|
+
arguments: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
interface ToolDef {
|
|
19
|
+
function?: {
|
|
20
|
+
name?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
parameters?: unknown;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** First sentence of a tool description, markup and parentheticals stripped. */
|
|
26
|
+
export declare function compactDescription(text: string, limit?: number): string;
|
|
27
|
+
/** System-prompt block declaring client tools and the call protocol. */
|
|
28
|
+
export declare function renderTools(tools: ToolDef[], descMode?: string, lang?: string): string;
|
|
29
|
+
/** Parse Devin's native tool-call syntax when the model emits it as text. */
|
|
30
|
+
export declare function parseNativeToolText(text: string): [string, ToolCall[]];
|
|
31
|
+
/**
|
|
32
|
+
* Remove leftover native-protocol wrappers from a plain answer.
|
|
33
|
+
* Only flattens same-line whitespace — paragraph breaks and markdown
|
|
34
|
+
* structure are the model's output, not noise to strip.
|
|
35
|
+
*/
|
|
36
|
+
export declare function stripProtocolNoise(text: string): string;
|
|
37
|
+
/** Parse the <<TOOL_CALL>> text protocol; returns (clean_text, calls). */
|
|
38
|
+
export declare function parseToolText(text: string): [string, ToolCall[]];
|
|
39
|
+
export {};
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-calling support: native wire declarations (field 10) as the primary
|
|
3
|
+
* channel, the <<TOOL_CALL>> text protocol as the fallback, plus the parser
|
|
4
|
+
* that turns either back into OpenAI tool_calls.
|
|
5
|
+
*
|
|
6
|
+
* Ported from devin_proxy/jailbreak/tools.py.
|
|
7
|
+
*/
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
export const TOOL_OPEN = "<<TOOL_CALL>>";
|
|
10
|
+
export const TOOL_CLOSE = "<<END_TOOL_CALL>>";
|
|
11
|
+
const NATIVE_OPEN = "<" + "|open" + "|>";
|
|
12
|
+
const NATIVE_CLOSE = "<" + "|close" + "|>";
|
|
13
|
+
const NATIVE_SEP = "<" + "|sep" + "|>";
|
|
14
|
+
const esc = (s) => s.replace(/[|]/g, "\\|");
|
|
15
|
+
/** First sentence of a tool description, markup and parentheticals stripped. */
|
|
16
|
+
export function compactDescription(text, limit = 200) {
|
|
17
|
+
let t = String(text ?? "").replace(/\s+/g, " ").trim();
|
|
18
|
+
t = t.replace(/`{1,3}/g, "");
|
|
19
|
+
t = t.replace(/<[^>]{1,40}>/g, " ");
|
|
20
|
+
t = t.replace(/\([^()]{0,200}\)/g, " ");
|
|
21
|
+
t = t.replace(/\s+/g, " ").trim();
|
|
22
|
+
let first = t.split(/(?<=[.!?\u2026])\s/)[0] ?? t;
|
|
23
|
+
if (first.length < 12 && t.length > first.length)
|
|
24
|
+
first = t;
|
|
25
|
+
return first.length > limit ? first.slice(0, limit).trimEnd() + "\u2026" : first;
|
|
26
|
+
}
|
|
27
|
+
/** System-prompt block declaring client tools and the call protocol. */
|
|
28
|
+
export function renderTools(tools, descMode = "full", lang = "en") {
|
|
29
|
+
const lines = lang === "es"
|
|
30
|
+
? [
|
|
31
|
+
"Llamadas a herramientas. Cuando la tarea necesite una herramienta, emite una llamada y para:",
|
|
32
|
+
TOOL_OPEN + ' {"name": "<tool>", "arguments": {<json arguments>}} ' + TOOL_CLOSE,
|
|
33
|
+
"No escribas nada m\u00e1s en ese turno; el resultado de la herramienta llega como el siguiente " +
|
|
34
|
+
"mensaje. Nunca inventes un resultado de herramienta.",
|
|
35
|
+
"CR\u00cdTICO: cualquier petici\u00f3n de buscar, leer, ejecutar, editar o comprobar algo en esta m\u00e1quina " +
|
|
36
|
+
"DEBE responderse con exactamente una llamada a herramienta \u2014 nunca describas el plan, " +
|
|
37
|
+
"nunca anuncies la b\u00fasqueda, nunca respondas de memoria. Las preguntas que no necesitan " +
|
|
38
|
+
"herramienta se responden normal.",
|
|
39
|
+
"",
|
|
40
|
+
"Herramientas disponibles:",
|
|
41
|
+
]
|
|
42
|
+
: [
|
|
43
|
+
"Tool calling. When the task needs a tool, output one call and stop:",
|
|
44
|
+
TOOL_OPEN + ' {"name": "<tool>", "arguments": {<json arguments>}} ' + TOOL_CLOSE,
|
|
45
|
+
"Output nothing else in that turn; the tool result arrives as the next " +
|
|
46
|
+
"message. Never invent a tool result.",
|
|
47
|
+
"CRITICAL: any request to find, read, run, edit or check anything on " +
|
|
48
|
+
"this machine MUST be answered with exactly one tool call \u2014 never " +
|
|
49
|
+
"describe the plan, never announce the search, never answer from " +
|
|
50
|
+
"memory. Questions that need no tool are answered normally.",
|
|
51
|
+
"",
|
|
52
|
+
"Available tools:",
|
|
53
|
+
];
|
|
54
|
+
for (const t of tools ?? []) {
|
|
55
|
+
const fn = t?.function ?? {};
|
|
56
|
+
const name = fn.name;
|
|
57
|
+
if (!name)
|
|
58
|
+
continue;
|
|
59
|
+
let desc = String(fn.description ?? "").trim();
|
|
60
|
+
if (descMode === "none")
|
|
61
|
+
desc = "";
|
|
62
|
+
else if (descMode === "compact")
|
|
63
|
+
desc = compactDescription(desc);
|
|
64
|
+
lines.push(desc ? `- ${name}: ${desc}` : `- ${name}`);
|
|
65
|
+
if (fn.parameters) {
|
|
66
|
+
lines.push(" parameters: " + JSON.stringify(fn.parameters).slice(0, 1500));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return lines.join("\n");
|
|
70
|
+
}
|
|
71
|
+
function coerceArg(raw, typ) {
|
|
72
|
+
const s = raw.trim();
|
|
73
|
+
const t = (typ ?? "string").toLowerCase();
|
|
74
|
+
if (["number", "integer", "int", "float", "double"].includes(t)) {
|
|
75
|
+
const n = /^-?\d+$/.test(s) ? parseInt(s, 10) : parseFloat(s);
|
|
76
|
+
return Number.isNaN(n) ? s : n;
|
|
77
|
+
}
|
|
78
|
+
if (["boolean", "bool"].includes(t)) {
|
|
79
|
+
return ["true", "1", "yes"].includes(s.toLowerCase());
|
|
80
|
+
}
|
|
81
|
+
if (["object", "array", "json"].includes(t)) {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(s);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return s;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return s;
|
|
90
|
+
}
|
|
91
|
+
/** Parse Devin's native tool-call syntax when the model emits it as text. */
|
|
92
|
+
export function parseNativeToolText(text) {
|
|
93
|
+
if (!text?.includes(NATIVE_OPEN + "call"))
|
|
94
|
+
return [text, []];
|
|
95
|
+
const calls = [];
|
|
96
|
+
const callRe = new RegExp(esc(NATIVE_OPEN) + 'call\\s+tool="([^"]+)"(.*?)' + esc(NATIVE_CLOSE) + "call", "gs");
|
|
97
|
+
for (const m of text.matchAll(callRe)) {
|
|
98
|
+
const [, name, body] = m;
|
|
99
|
+
const args = {};
|
|
100
|
+
const argRe = new RegExp(esc(NATIVE_OPEN) +
|
|
101
|
+
'argument\\s+key="([^"]+)"(?:\\s+type="([^"]+)")?\\s*' +
|
|
102
|
+
esc(NATIVE_SEP) +
|
|
103
|
+
"(.*?)" +
|
|
104
|
+
esc(NATIVE_CLOSE) +
|
|
105
|
+
"argument", "gs");
|
|
106
|
+
for (const am of body.matchAll(argRe)) {
|
|
107
|
+
args[am[1]] = coerceArg(am[3], am[2]);
|
|
108
|
+
}
|
|
109
|
+
calls.push({
|
|
110
|
+
id: "call_" + randomUUID().replace(/-/g, "").slice(0, 12),
|
|
111
|
+
type: "function",
|
|
112
|
+
function: { name, arguments: JSON.stringify(args) },
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (!calls.length)
|
|
116
|
+
return [text, []];
|
|
117
|
+
let clean = text;
|
|
118
|
+
let start = clean.indexOf(NATIVE_OPEN + "tools");
|
|
119
|
+
if (start < 0)
|
|
120
|
+
start = clean.indexOf(NATIVE_OPEN + "call");
|
|
121
|
+
let end = clean.indexOf(NATIVE_CLOSE + "tools", start);
|
|
122
|
+
if (end >= 0) {
|
|
123
|
+
end += (NATIVE_CLOSE + "tools").length;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const last = clean.lastIndexOf(NATIVE_CLOSE + "call");
|
|
127
|
+
end = last >= 0 ? last + (NATIVE_CLOSE + "call").length : clean.length;
|
|
128
|
+
}
|
|
129
|
+
clean = clean.slice(0, start) + clean.slice(end);
|
|
130
|
+
clean = clean.split(NATIVE_OPEN + "message").join("").split(NATIVE_CLOSE + "message").join("");
|
|
131
|
+
clean = clean.split(NATIVE_OPEN).join(" ").split(NATIVE_CLOSE).join(" ").split(NATIVE_SEP).join(" ");
|
|
132
|
+
return [clean.trim(), calls];
|
|
133
|
+
}
|
|
134
|
+
/** Best-effort decode of a tool-call body that is not valid JSON. */
|
|
135
|
+
function salvageToolObj(raw) {
|
|
136
|
+
const s = (raw ?? "").trim();
|
|
137
|
+
if (!s)
|
|
138
|
+
return null;
|
|
139
|
+
try {
|
|
140
|
+
const obj = JSON.parse(s);
|
|
141
|
+
if (obj && typeof obj === "object" && !Array.isArray(obj))
|
|
142
|
+
return obj;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
/* fall through */
|
|
146
|
+
}
|
|
147
|
+
for (let end = s.length - 1; end > Math.max(0, s.length - 400); end--) {
|
|
148
|
+
try {
|
|
149
|
+
const obj = JSON.parse(s.slice(0, end));
|
|
150
|
+
if (obj && typeof obj === "object" && !Array.isArray(obj) && (obj.name || obj.tool))
|
|
151
|
+
return obj;
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const m = /"name"\s*:\s*"([^"]+)"/.exec(s);
|
|
158
|
+
if (!m)
|
|
159
|
+
return null;
|
|
160
|
+
let args = {};
|
|
161
|
+
const am = /"arguments"\s*:\s*/.exec(s);
|
|
162
|
+
if (am) {
|
|
163
|
+
const rest = s.slice(am.index + am[0].length).trimStart();
|
|
164
|
+
if (rest.startsWith("{")) {
|
|
165
|
+
let depth = 0;
|
|
166
|
+
let instr = false;
|
|
167
|
+
let esc2 = false;
|
|
168
|
+
const out = [];
|
|
169
|
+
for (const ch of rest) {
|
|
170
|
+
out.push(ch);
|
|
171
|
+
if (esc2) {
|
|
172
|
+
esc2 = false;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (ch === "\\") {
|
|
176
|
+
esc2 = true;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (ch === '"') {
|
|
180
|
+
instr = !instr;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (instr)
|
|
184
|
+
continue;
|
|
185
|
+
if (ch === "{")
|
|
186
|
+
depth++;
|
|
187
|
+
else if (ch === "}") {
|
|
188
|
+
depth--;
|
|
189
|
+
if (depth === 0)
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
args = JSON.parse(out.join(""));
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
args = {};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { name: m[1], arguments: args };
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Remove leftover native-protocol wrappers from a plain answer.
|
|
205
|
+
* Only flattens same-line whitespace — paragraph breaks and markdown
|
|
206
|
+
* structure are the model's output, not noise to strip.
|
|
207
|
+
*/
|
|
208
|
+
export function stripProtocolNoise(text) {
|
|
209
|
+
if (!text)
|
|
210
|
+
return text;
|
|
211
|
+
let t = text;
|
|
212
|
+
for (const tok of ["tools", "call", "argument", "message"]) {
|
|
213
|
+
t = t.split(NATIVE_OPEN + tok).join(" ").split(NATIVE_CLOSE + tok).join(" ");
|
|
214
|
+
}
|
|
215
|
+
t = t.split(NATIVE_OPEN).join(" ").split(NATIVE_CLOSE).join(" ").split(NATIVE_SEP).join(" ");
|
|
216
|
+
t = t.replace(/[^\S\n]{2,}/g, " ");
|
|
217
|
+
t = t.replace(/\n{3,}/g, "\n\n");
|
|
218
|
+
return t.trim();
|
|
219
|
+
}
|
|
220
|
+
/** Parse the <<TOOL_CALL>> text protocol; returns (clean_text, calls). */
|
|
221
|
+
export function parseToolText(text) {
|
|
222
|
+
if (!text?.includes(TOOL_OPEN))
|
|
223
|
+
return [text, []];
|
|
224
|
+
const calls = [];
|
|
225
|
+
const re = new RegExp(TOOL_OPEN.replace(/[<>]/g, "\\$&") + "\\s*(.*?)\\s*" + TOOL_CLOSE.replace(/[<>]/g, "\\$&"), "gs");
|
|
226
|
+
for (const m of text.matchAll(re)) {
|
|
227
|
+
const obj = salvageToolObj(m[1]);
|
|
228
|
+
if (!obj)
|
|
229
|
+
continue;
|
|
230
|
+
const name = String(obj.name ?? obj.tool ?? "");
|
|
231
|
+
if (!name)
|
|
232
|
+
continue;
|
|
233
|
+
const args = obj.arguments;
|
|
234
|
+
const argsJson = typeof args === "string" ? args : JSON.stringify(args ?? {});
|
|
235
|
+
calls.push({
|
|
236
|
+
id: "call_" + randomUUID().replace(/-/g, "").slice(0, 12),
|
|
237
|
+
type: "function",
|
|
238
|
+
function: { name, arguments: argsJson },
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
const clean = text
|
|
242
|
+
.replace(new RegExp(TOOL_OPEN.replace(/[<>]/g, "\\$&") + ".*?" + TOOL_CLOSE.replace(/[<>]/g, "\\$&"), "gs"), "")
|
|
243
|
+
.trim();
|
|
244
|
+
return [clean, calls];
|
|
245
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI <-> Devin message translation.
|
|
3
|
+
*
|
|
4
|
+
* The Devin wire has no system role — system content folds into the next
|
|
5
|
+
* user turn; frame notes land as a SYSTEM-tagged mid-history user turn
|
|
6
|
+
* right before the last user message.
|
|
7
|
+
*
|
|
8
|
+
* Ported from devin_proxy/translate.py.
|
|
9
|
+
*/
|
|
10
|
+
import type { ChatHistoryItem } from "devin-core";
|
|
11
|
+
interface OpenAIMessage {
|
|
12
|
+
role?: string;
|
|
13
|
+
content?: string | Array<{
|
|
14
|
+
type?: string;
|
|
15
|
+
text?: string;
|
|
16
|
+
image_url?: {
|
|
17
|
+
url?: string;
|
|
18
|
+
};
|
|
19
|
+
}>;
|
|
20
|
+
tool_calls?: Array<{
|
|
21
|
+
id?: string;
|
|
22
|
+
function?: {
|
|
23
|
+
name?: string;
|
|
24
|
+
arguments?: string;
|
|
25
|
+
};
|
|
26
|
+
}>;
|
|
27
|
+
tool_call_id?: string;
|
|
28
|
+
[k: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Fold system messages into the following user turn; convert to
|
|
32
|
+
* ChatHistoryItem[].
|
|
33
|
+
*/
|
|
34
|
+
export declare function openaiToWire(messages: OpenAIMessage[], opts?: {
|
|
35
|
+
frameNote?: string;
|
|
36
|
+
imgMaxBytes?: number;
|
|
37
|
+
}): {
|
|
38
|
+
system: string;
|
|
39
|
+
prompts: ChatHistoryItem[];
|
|
40
|
+
};
|
|
41
|
+
export {};
|