@agi-cli/server 0.1.63 → 0.1.65
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agi-cli/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.65",
|
|
4
4
|
"description": "HTTP API server for AGI CLI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"typecheck": "tsc --noEmit"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@agi-cli/sdk": "0.1.
|
|
33
|
-
"@agi-cli/database": "0.1.
|
|
32
|
+
"@agi-cli/sdk": "0.1.65",
|
|
33
|
+
"@agi-cli/database": "0.1.65",
|
|
34
34
|
"drizzle-orm": "^0.44.5",
|
|
35
35
|
"hono": "^4.9.9"
|
|
36
36
|
},
|
|
@@ -4,18 +4,29 @@ import type { ModelMessage } from 'ai';
|
|
|
4
4
|
* Adds cache control to messages for prompt caching optimization.
|
|
5
5
|
* Anthropic supports caching for system messages, tools, and long context.
|
|
6
6
|
*/
|
|
7
|
+
type CachedSystemValue =
|
|
8
|
+
| string
|
|
9
|
+
| undefined
|
|
10
|
+
| Array<{
|
|
11
|
+
type: 'text';
|
|
12
|
+
text: string;
|
|
13
|
+
cache_control?: { type: 'ephemeral' };
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
type TextContentPartWithProviderOptions = {
|
|
17
|
+
providerOptions?: {
|
|
18
|
+
anthropic?: { cacheControl?: { type: 'ephemeral' } };
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
};
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
};
|
|
23
|
+
|
|
7
24
|
export function addCacheControl(
|
|
8
25
|
provider: string,
|
|
9
26
|
system: string | undefined,
|
|
10
27
|
messages: ModelMessage[],
|
|
11
28
|
): {
|
|
12
|
-
system?:
|
|
13
|
-
| string
|
|
14
|
-
| Array<{
|
|
15
|
-
type: 'text';
|
|
16
|
-
text: string;
|
|
17
|
-
cache_control?: { type: 'ephemeral' };
|
|
18
|
-
}>;
|
|
29
|
+
system?: CachedSystemValue;
|
|
19
30
|
messages: ModelMessage[];
|
|
20
31
|
} {
|
|
21
32
|
// Only Anthropic supports prompt caching currently
|
|
@@ -24,7 +35,7 @@ export function addCacheControl(
|
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
// Convert system to cacheable format if it's long enough
|
|
27
|
-
let cachedSystem:
|
|
38
|
+
let cachedSystem: CachedSystemValue = system;
|
|
28
39
|
if (system && system.length > 1024) {
|
|
29
40
|
// Anthropic requires 1024+ tokens for Claude Sonnet/Opus
|
|
30
41
|
cachedSystem = [
|
|
@@ -60,8 +71,16 @@ export function addCacheControl(
|
|
|
60
71
|
if (Array.isArray(targetMsg.content)) {
|
|
61
72
|
// Add cache control to the last content part of that message
|
|
62
73
|
const lastPart = targetMsg.content[targetMsg.content.length - 1];
|
|
63
|
-
if (
|
|
64
|
-
|
|
74
|
+
if (
|
|
75
|
+
lastPart &&
|
|
76
|
+
typeof lastPart === 'object' &&
|
|
77
|
+
'type' in lastPart &&
|
|
78
|
+
lastPart.type === 'text'
|
|
79
|
+
) {
|
|
80
|
+
const textPart =
|
|
81
|
+
lastPart as unknown as TextContentPartWithProviderOptions;
|
|
82
|
+
textPart.providerOptions = {
|
|
83
|
+
...textPart.providerOptions,
|
|
65
84
|
anthropic: { cacheControl: { type: 'ephemeral' } },
|
|
66
85
|
};
|
|
67
86
|
}
|
|
@@ -3,7 +3,7 @@ import { messages, messageParts, sessions } from '@agi-cli/database/schema';
|
|
|
3
3
|
import { eq } from 'drizzle-orm';
|
|
4
4
|
import type { RunOpts } from './session-queue.ts';
|
|
5
5
|
|
|
6
|
-
type UsageData = {
|
|
6
|
+
export type UsageData = {
|
|
7
7
|
inputTokens?: number;
|
|
8
8
|
outputTokens?: number;
|
|
9
9
|
totalTokens?: number;
|
|
@@ -11,13 +11,20 @@ type UsageData = {
|
|
|
11
11
|
reasoningTokens?: number;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
export type ProviderMetadata = Record<string, unknown> & {
|
|
15
|
+
openai?: {
|
|
16
|
+
cachedPromptTokens?: number;
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
14
21
|
/**
|
|
15
22
|
* Updates session token counts incrementally after each step.
|
|
16
23
|
* Note: onStepFinish.usage is CUMULATIVE per message, so we compute DELTA and add to session.
|
|
17
24
|
*/
|
|
18
25
|
export async function updateSessionTokensIncremental(
|
|
19
26
|
usage: UsageData,
|
|
20
|
-
providerMetadata:
|
|
27
|
+
providerMetadata: ProviderMetadata | undefined,
|
|
21
28
|
opts: RunOpts,
|
|
22
29
|
db: Awaited<ReturnType<typeof getDb>>,
|
|
23
30
|
) {
|
|
@@ -129,7 +136,7 @@ export async function updateSessionTokens(
|
|
|
129
136
|
*/
|
|
130
137
|
export async function updateMessageTokensIncremental(
|
|
131
138
|
usage: UsageData,
|
|
132
|
-
providerMetadata:
|
|
139
|
+
providerMetadata: ProviderMetadata | undefined,
|
|
133
140
|
opts: RunOpts,
|
|
134
141
|
db: Awaited<ReturnType<typeof getDb>>,
|
|
135
142
|
) {
|
|
@@ -187,7 +194,7 @@ export async function updateMessageTokensIncremental(
|
|
|
187
194
|
* Token usage is tracked incrementally via updateMessageTokensIncremental().
|
|
188
195
|
*/
|
|
189
196
|
export async function completeAssistantMessage(
|
|
190
|
-
|
|
197
|
+
_fin: {
|
|
191
198
|
usage?: {
|
|
192
199
|
inputTokens?: number;
|
|
193
200
|
outputTokens?: number;
|
package/src/runtime/runner.ts
CHANGED
|
@@ -7,26 +7,17 @@ import { estimateModelCostUsd } from '@agi-cli/sdk';
|
|
|
7
7
|
import { toErrorPayload } from './error-handling.ts';
|
|
8
8
|
import type { RunOpts } from './session-queue.ts';
|
|
9
9
|
import type { ToolAdapterContext } from '../tools/adapter.ts';
|
|
10
|
+
import type { ProviderMetadata, UsageData } from './db-operations.ts';
|
|
10
11
|
|
|
11
12
|
type StepFinishEvent = {
|
|
12
|
-
usage?:
|
|
13
|
-
inputTokens?: number;
|
|
14
|
-
outputTokens?: number;
|
|
15
|
-
totalTokens?: number;
|
|
16
|
-
cachedInputTokens?: number;
|
|
17
|
-
reasoningTokens?: number;
|
|
18
|
-
};
|
|
13
|
+
usage?: UsageData;
|
|
19
14
|
finishReason?: string;
|
|
20
15
|
response?: unknown;
|
|
21
|
-
experimental_providerMetadata?:
|
|
16
|
+
experimental_providerMetadata?: ProviderMetadata;
|
|
22
17
|
};
|
|
23
18
|
|
|
24
19
|
type FinishEvent = {
|
|
25
|
-
usage?:
|
|
26
|
-
inputTokens?: number;
|
|
27
|
-
outputTokens?: number;
|
|
28
|
-
totalTokens?: number;
|
|
29
|
-
};
|
|
20
|
+
usage?: Pick<UsageData, 'inputTokens' | 'outputTokens' | 'totalTokens'>;
|
|
30
21
|
finishReason?: string;
|
|
31
22
|
};
|
|
32
23
|
|
|
@@ -47,14 +38,14 @@ export function createStepFinishHandler(
|
|
|
47
38
|
updateAccumulated: (text: string) => void,
|
|
48
39
|
incrementStepIndex: () => number,
|
|
49
40
|
updateSessionTokensIncrementalFn: (
|
|
50
|
-
usage:
|
|
51
|
-
providerMetadata:
|
|
41
|
+
usage: UsageData,
|
|
42
|
+
providerMetadata: ProviderMetadata | undefined,
|
|
52
43
|
opts: RunOpts,
|
|
53
44
|
db: Awaited<ReturnType<typeof getDb>>,
|
|
54
45
|
) => Promise<void>,
|
|
55
46
|
updateMessageTokensIncrementalFn: (
|
|
56
|
-
usage:
|
|
57
|
-
providerMetadata:
|
|
47
|
+
usage: UsageData,
|
|
48
|
+
providerMetadata: ProviderMetadata | undefined,
|
|
58
49
|
opts: RunOpts,
|
|
59
50
|
db: Awaited<ReturnType<typeof getDb>>,
|
|
60
51
|
) => Promise<void>,
|