@workser/engine 0.1.9 → 0.1.10
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/src/model.d.ts +23 -0
- package/dist/src/model.js +32 -1
- package/package.json +1 -1
package/dist/src/model.d.ts
CHANGED
|
@@ -39,6 +39,28 @@ export declare class ModelError extends Error {
|
|
|
39
39
|
readonly status: number;
|
|
40
40
|
constructor(message: string, status: number);
|
|
41
41
|
}
|
|
42
|
+
type WirePart = {
|
|
43
|
+
type: 'text';
|
|
44
|
+
text: string;
|
|
45
|
+
cache_control?: {
|
|
46
|
+
type: 'ephemeral';
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
type WireMessage = Omit<ChatMessage, 'content'> & {
|
|
50
|
+
content: string | WirePart[] | null;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The messages as sent, with prompt-cache breakpoints for a route that needs them.
|
|
54
|
+
*
|
|
55
|
+
* Every turn resends the whole conversation; without a marker an Anthropic model
|
|
56
|
+
* bills all of it at the full input price each time (a 40-turn job cost about six
|
|
57
|
+
* times what a cached one does). Caching is a prefix match, so the markers go at
|
|
58
|
+
* the end of what stays byte-identical: the system prompt (the tool list is sent
|
|
59
|
+
* before it, so it is covered too) and the two newest user or tool turns, which
|
|
60
|
+
* the next turn then reads back. Three markers of Anthropic's four. The
|
|
61
|
+
* session's own messages are never changed; only the request body is.
|
|
62
|
+
*/
|
|
63
|
+
export declare function cachedMessages(model: string, messages: ChatMessage[]): WireMessage[];
|
|
42
64
|
export declare function chat(input: {
|
|
43
65
|
baseUrl: string;
|
|
44
66
|
key: string;
|
|
@@ -60,3 +82,4 @@ export declare function chat(input: {
|
|
|
60
82
|
*/
|
|
61
83
|
onReasoning?: (text: string) => void;
|
|
62
84
|
}): Promise<ChatResult>;
|
|
85
|
+
export {};
|
package/dist/src/model.js
CHANGED
|
@@ -6,6 +6,37 @@ export class ModelError extends Error {
|
|
|
6
6
|
this.name = 'ModelError';
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
/** Routes that cache a prompt only where it is marked (others cache on their own, with no marker). */
|
|
10
|
+
const MARKED_CACHE_PREFIXES = ['anthropic/', 'google/'];
|
|
11
|
+
/**
|
|
12
|
+
* The messages as sent, with prompt-cache breakpoints for a route that needs them.
|
|
13
|
+
*
|
|
14
|
+
* Every turn resends the whole conversation; without a marker an Anthropic model
|
|
15
|
+
* bills all of it at the full input price each time (a 40-turn job cost about six
|
|
16
|
+
* times what a cached one does). Caching is a prefix match, so the markers go at
|
|
17
|
+
* the end of what stays byte-identical: the system prompt (the tool list is sent
|
|
18
|
+
* before it, so it is covered too) and the two newest user or tool turns, which
|
|
19
|
+
* the next turn then reads back. Three markers of Anthropic's four. The
|
|
20
|
+
* session's own messages are never changed; only the request body is.
|
|
21
|
+
*/
|
|
22
|
+
export function cachedMessages(model, messages) {
|
|
23
|
+
if (!MARKED_CACHE_PREFIXES.some((prefix) => model.startsWith(prefix)))
|
|
24
|
+
return messages;
|
|
25
|
+
const marked = new Set();
|
|
26
|
+
const system = messages.findIndex((one) => one.role === 'system');
|
|
27
|
+
if (system >= 0)
|
|
28
|
+
marked.add(system);
|
|
29
|
+
for (let i = messages.length - 1, n = 0; i >= 0 && n < 2; i--) {
|
|
30
|
+
const role = messages[i].role;
|
|
31
|
+
if ((role === 'user' || role === 'tool') && messages[i].content) {
|
|
32
|
+
marked.add(i);
|
|
33
|
+
n++;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return messages.map((one, i) => marked.has(i) && one.content
|
|
37
|
+
? { ...one, content: [{ type: 'text', text: one.content, cache_control: { type: 'ephemeral' } }] }
|
|
38
|
+
: one);
|
|
39
|
+
}
|
|
9
40
|
export async function chat(input) {
|
|
10
41
|
const doFetch = input.fetch ?? fetch;
|
|
11
42
|
const url = `${input.baseUrl.replace(/\/+$/, '')}/chat/completions`;
|
|
@@ -19,7 +50,7 @@ export async function chat(input) {
|
|
|
19
50
|
headers: { authorization: `Bearer ${input.key}`, 'content-type': 'application/json' },
|
|
20
51
|
body: JSON.stringify({
|
|
21
52
|
model: input.model,
|
|
22
|
-
messages: input.messages,
|
|
53
|
+
messages: cachedMessages(input.model, input.messages),
|
|
23
54
|
...(input.tools.length ? { tools: input.tools, tool_choice: 'auto' } : {}),
|
|
24
55
|
usage: { include: true },
|
|
25
56
|
...(stream ? { stream: true, stream_options: { include_usage: true } } : {}),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workser/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Workser's coding engine for the 'loop in our service' mode: OpenCode's agent contract (its prompts, tool schemas and edit semantics, vendored from v1.18.23) running on Node, with every file and shell action going through a Place — so the machine can be a remote sandbox and no model key is ever on it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|