@peer-agent/runtime-core 0.0.1-beta.43
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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/capability-registry.d.ts +15 -0
- package/dist/capability-registry.js +88 -0
- package/dist/compaction-progress.d.ts +51 -0
- package/dist/compaction-progress.js +83 -0
- package/dist/context-accounting-lifecycle.d.ts +41 -0
- package/dist/context-accounting-lifecycle.js +90 -0
- package/dist/context-accounting-pipeline.d.ts +98 -0
- package/dist/context-accounting-pipeline.js +561 -0
- package/dist/context-compaction.d.ts +70 -0
- package/dist/context-compaction.js +192 -0
- package/dist/context-projection-lifecycle.d.ts +32 -0
- package/dist/context-projection-lifecycle.js +65 -0
- package/dist/context-projection.d.ts +74 -0
- package/dist/context-projection.js +183 -0
- package/dist/contracts.d.ts +218 -0
- package/dist/contracts.js +1 -0
- package/dist/conversation-history-projector.d.ts +52 -0
- package/dist/conversation-history-projector.js +472 -0
- package/dist/decision.d.ts +4 -0
- package/dist/decision.js +23 -0
- package/dist/evidence.d.ts +64 -0
- package/dist/evidence.js +127 -0
- package/dist/hooks.d.ts +5 -0
- package/dist/hooks.js +4 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/local-tool-contracts.d.ts +60 -0
- package/dist/local-tool-contracts.js +64 -0
- package/dist/microcompact.d.ts +38 -0
- package/dist/microcompact.js +354 -0
- package/dist/permission.d.ts +18 -0
- package/dist/permission.js +50 -0
- package/dist/projection-materializers.d.ts +18 -0
- package/dist/projection-materializers.js +34 -0
- package/dist/projection.d.ts +15 -0
- package/dist/projection.js +78 -0
- package/dist/runtime-usage-accounting.d.ts +19 -0
- package/dist/runtime-usage-accounting.js +79 -0
- package/package.json +47 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
function toolDescription(tool) {
|
|
2
|
+
return tool.description ?? '';
|
|
3
|
+
}
|
|
4
|
+
function toolInputSchema(tool) {
|
|
5
|
+
return tool.inputSchema ?? {
|
|
6
|
+
type: 'object',
|
|
7
|
+
properties: {},
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export function materializeOpenAITools(projection) {
|
|
11
|
+
return projection.tools.map((tool) => ({
|
|
12
|
+
type: 'function',
|
|
13
|
+
function: {
|
|
14
|
+
name: tool.name,
|
|
15
|
+
description: toolDescription(tool),
|
|
16
|
+
parameters: toolInputSchema(tool),
|
|
17
|
+
},
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
export function materializeAnthropicTools(projection) {
|
|
21
|
+
return projection.tools.map((tool) => ({
|
|
22
|
+
name: tool.name,
|
|
23
|
+
description: toolDescription(tool),
|
|
24
|
+
input_schema: toolInputSchema(tool),
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
export const openAIProjectionMaterializer = {
|
|
28
|
+
providerFamily: 'openai',
|
|
29
|
+
materialize: materializeOpenAITools,
|
|
30
|
+
};
|
|
31
|
+
export const anthropicProjectionMaterializer = {
|
|
32
|
+
providerFamily: 'anthropic',
|
|
33
|
+
materialize: materializeAnthropicTools,
|
|
34
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RuntimeJsonObject, RuntimeModeScope, RuntimeProjection, RuntimeToolDefinition } from './contracts.ts';
|
|
2
|
+
export type RuntimeProjectionErrorCode = 'invalid_tool_definition' | 'duplicate_tool_name';
|
|
3
|
+
export declare class RuntimeProjectionError extends Error {
|
|
4
|
+
readonly code: RuntimeProjectionErrorCode;
|
|
5
|
+
constructor(message: string, code: RuntimeProjectionErrorCode);
|
|
6
|
+
}
|
|
7
|
+
export interface CreateRuntimeProjectionOptions {
|
|
8
|
+
readonly mode?: RuntimeModeScope | null;
|
|
9
|
+
readonly createdAt?: string;
|
|
10
|
+
readonly now?: () => string;
|
|
11
|
+
readonly metadata?: RuntimeJsonObject;
|
|
12
|
+
}
|
|
13
|
+
export declare function isRuntimeToolAvailableInMode(tool: RuntimeToolDefinition, mode?: RuntimeModeScope | null): boolean;
|
|
14
|
+
export declare function filterRuntimeToolsForMode(tools: readonly RuntimeToolDefinition[], mode?: RuntimeModeScope | null): readonly RuntimeToolDefinition[];
|
|
15
|
+
export declare function createRuntimeProjection(tools: readonly RuntimeToolDefinition[], options?: CreateRuntimeProjectionOptions): RuntimeProjection;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export class RuntimeProjectionError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(message, code) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'RuntimeProjectionError';
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function assertToolDefinition(tool) {
|
|
10
|
+
if (!tool || typeof tool !== 'object') {
|
|
11
|
+
throw new RuntimeProjectionError('Runtime tool definition must be an object.', 'invalid_tool_definition');
|
|
12
|
+
}
|
|
13
|
+
if (typeof tool.name !== 'string' || tool.name.trim().length === 0) {
|
|
14
|
+
throw new RuntimeProjectionError('Runtime tool definition must declare a non-empty name.', 'invalid_tool_definition');
|
|
15
|
+
}
|
|
16
|
+
if (typeof tool.capabilityId !== 'string' || tool.capabilityId.trim().length === 0) {
|
|
17
|
+
throw new RuntimeProjectionError(`Runtime tool definition ${tool.name} must declare a non-empty capabilityId.`, 'invalid_tool_definition');
|
|
18
|
+
}
|
|
19
|
+
if (tool.description !== undefined && typeof tool.description !== 'string') {
|
|
20
|
+
throw new RuntimeProjectionError(`Runtime tool definition ${tool.name} must use a string description.`, 'invalid_tool_definition');
|
|
21
|
+
}
|
|
22
|
+
if (tool.modeScopes !== undefined
|
|
23
|
+
&& (!Array.isArray(tool.modeScopes)
|
|
24
|
+
|| tool.modeScopes.some((mode) => typeof mode !== 'string' || mode.trim().length === 0))) {
|
|
25
|
+
throw new RuntimeProjectionError(`Runtime tool definition ${tool.name} must use non-empty string mode scopes.`, 'invalid_tool_definition');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function freezeToolDefinition(tool) {
|
|
29
|
+
const modeScopes = tool.modeScopes === undefined
|
|
30
|
+
? undefined
|
|
31
|
+
: Object.freeze([...tool.modeScopes]);
|
|
32
|
+
const metadata = tool.metadata === undefined
|
|
33
|
+
? undefined
|
|
34
|
+
: Object.freeze({ ...tool.metadata });
|
|
35
|
+
return Object.freeze({
|
|
36
|
+
...tool,
|
|
37
|
+
...(modeScopes === undefined ? {} : { modeScopes }),
|
|
38
|
+
...(metadata === undefined ? {} : { metadata }),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export function isRuntimeToolAvailableInMode(tool, mode) {
|
|
42
|
+
if (mode == null)
|
|
43
|
+
return true;
|
|
44
|
+
const modeScopes = tool.modeScopes;
|
|
45
|
+
if (!Array.isArray(modeScopes) || modeScopes.length === 0) {
|
|
46
|
+
return mode === 'chat' || mode === 'plan' || mode === 'goal';
|
|
47
|
+
}
|
|
48
|
+
return modeScopes.includes(mode);
|
|
49
|
+
}
|
|
50
|
+
export function filterRuntimeToolsForMode(tools, mode) {
|
|
51
|
+
return tools.filter((tool) => isRuntimeToolAvailableInMode(tool, mode));
|
|
52
|
+
}
|
|
53
|
+
export function createRuntimeProjection(tools, options = {}) {
|
|
54
|
+
if (!Array.isArray(tools)) {
|
|
55
|
+
throw new RuntimeProjectionError('Runtime projection requires an array of tool definitions.', 'invalid_tool_definition');
|
|
56
|
+
}
|
|
57
|
+
const toolNames = new Set();
|
|
58
|
+
const normalizedTools = tools.map((tool) => {
|
|
59
|
+
assertToolDefinition(tool);
|
|
60
|
+
if (toolNames.has(tool.name)) {
|
|
61
|
+
throw new RuntimeProjectionError(`Duplicate runtime tool definition: ${tool.name}`, 'duplicate_tool_name');
|
|
62
|
+
}
|
|
63
|
+
toolNames.add(tool.name);
|
|
64
|
+
return freezeToolDefinition(tool);
|
|
65
|
+
});
|
|
66
|
+
const projectedTools = Object.freeze([
|
|
67
|
+
...filterRuntimeToolsForMode(normalizedTools, options.mode),
|
|
68
|
+
]);
|
|
69
|
+
const metadata = options.metadata === undefined
|
|
70
|
+
? undefined
|
|
71
|
+
: Object.freeze({ ...options.metadata });
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
tools: projectedTools,
|
|
74
|
+
...(options.mode == null ? {} : { mode: options.mode }),
|
|
75
|
+
createdAt: options.createdAt ?? options.now?.() ?? new Date().toISOString(),
|
|
76
|
+
...(metadata === undefined ? {} : { metadata }),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ProviderRequestUsage, RuntimeTurnUsage, UsageAmounts } from '@peer-agent/protocol';
|
|
2
|
+
export type ProviderUsageInput = Readonly<{
|
|
3
|
+
[Key in keyof UsageAmounts]?: number | null;
|
|
4
|
+
}> | null | undefined;
|
|
5
|
+
export interface RuntimeUsageAccountingSnapshot {
|
|
6
|
+
readonly providerRequestCount: number;
|
|
7
|
+
readonly lastRequest: ProviderRequestUsage | null;
|
|
8
|
+
readonly turnTotal: RuntimeTurnUsage;
|
|
9
|
+
}
|
|
10
|
+
export interface RuntimeUsageAccounting {
|
|
11
|
+
observeProviderRequest(usage?: ProviderUsageInput, metadata?: {
|
|
12
|
+
readonly requestFingerprint?: string;
|
|
13
|
+
readonly requestPurpose?: 'agent' | 'compaction_summary';
|
|
14
|
+
/** Auxiliary requests are billable but must not replace context truth. */
|
|
15
|
+
readonly capacityBearing?: boolean;
|
|
16
|
+
}): RuntimeUsageAccountingSnapshot;
|
|
17
|
+
snapshot(): RuntimeUsageAccountingSnapshot;
|
|
18
|
+
}
|
|
19
|
+
export declare function createRuntimeUsageAccounting(): RuntimeUsageAccounting;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
function token(value) {
|
|
2
|
+
const parsed = Number(value);
|
|
3
|
+
return Number.isFinite(parsed) && parsed >= 0 ? Math.floor(parsed) : 0;
|
|
4
|
+
}
|
|
5
|
+
function normalizeUsage(usage) {
|
|
6
|
+
const inputTokens = token(usage.inputTokens);
|
|
7
|
+
const outputTokens = token(usage.outputTokens);
|
|
8
|
+
const cacheReadTokens = token(usage.cacheReadTokens);
|
|
9
|
+
const cacheWriteTokens = token(usage.cacheWriteTokens);
|
|
10
|
+
return {
|
|
11
|
+
inputTokens,
|
|
12
|
+
outputTokens,
|
|
13
|
+
cacheReadTokens,
|
|
14
|
+
cacheWriteTokens,
|
|
15
|
+
// Provider `totalTokens` is not canonical: some APIs include cached input
|
|
16
|
+
// there while exposing cache separately. Peer adapters normalize cache as
|
|
17
|
+
// disjoint dimensions, so this sum is stable across providers.
|
|
18
|
+
totalTokens: inputTokens + outputTokens + cacheReadTokens + cacheWriteTokens,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function hasUsage(usage) {
|
|
22
|
+
return usage.inputTokens > 0
|
|
23
|
+
|| usage.outputTokens > 0
|
|
24
|
+
|| usage.cacheReadTokens > 0
|
|
25
|
+
|| usage.cacheWriteTokens > 0
|
|
26
|
+
|| usage.totalTokens > 0;
|
|
27
|
+
}
|
|
28
|
+
export function createRuntimeUsageAccounting() {
|
|
29
|
+
let providerRequestCount = 0;
|
|
30
|
+
let lastRequest = null;
|
|
31
|
+
let totals = {
|
|
32
|
+
inputTokens: 0,
|
|
33
|
+
outputTokens: 0,
|
|
34
|
+
cacheReadTokens: 0,
|
|
35
|
+
cacheWriteTokens: 0,
|
|
36
|
+
totalTokens: 0,
|
|
37
|
+
};
|
|
38
|
+
function snapshot() {
|
|
39
|
+
return {
|
|
40
|
+
providerRequestCount,
|
|
41
|
+
lastRequest: lastRequest ? { ...lastRequest } : null,
|
|
42
|
+
turnTotal: {
|
|
43
|
+
usageScope: 'runtime_turn',
|
|
44
|
+
providerRequestCount,
|
|
45
|
+
...totals,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
observeProviderRequest(usage, metadata = {}) {
|
|
51
|
+
providerRequestCount += 1;
|
|
52
|
+
if (usage) {
|
|
53
|
+
const normalized = normalizeUsage(usage);
|
|
54
|
+
if (hasUsage(normalized)) {
|
|
55
|
+
const request = {
|
|
56
|
+
usageScope: 'provider_request',
|
|
57
|
+
requestIndex: providerRequestCount,
|
|
58
|
+
requestPurpose: metadata.requestPurpose ?? 'agent',
|
|
59
|
+
...(metadata.requestFingerprint
|
|
60
|
+
? { requestFingerprint: metadata.requestFingerprint }
|
|
61
|
+
: {}),
|
|
62
|
+
...normalized,
|
|
63
|
+
};
|
|
64
|
+
if (metadata.capacityBearing !== false)
|
|
65
|
+
lastRequest = request;
|
|
66
|
+
totals = {
|
|
67
|
+
inputTokens: totals.inputTokens + normalized.inputTokens,
|
|
68
|
+
outputTokens: totals.outputTokens + normalized.outputTokens,
|
|
69
|
+
cacheReadTokens: totals.cacheReadTokens + normalized.cacheReadTokens,
|
|
70
|
+
cacheWriteTokens: totals.cacheWriteTokens + normalized.cacheWriteTokens,
|
|
71
|
+
totalTokens: totals.totalTokens + normalized.totalTokens,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return snapshot();
|
|
76
|
+
},
|
|
77
|
+
snapshot,
|
|
78
|
+
};
|
|
79
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peer-agent/runtime-core",
|
|
3
|
+
"version": "0.0.1-beta.43",
|
|
4
|
+
"description": "Host-neutral capability, permission, projection, and Evidence primitives for Peer Agent runtimes.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=20"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/yinLiangDream/Peer-Agent.git",
|
|
14
|
+
"directory": "packages/runtime-core"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/yinLiangDream/Peer-Agent#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/yinLiangDream/Peer-Agent/issues"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@peer-agent/protocol": "0.0.1-beta.43"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"typescript": "^5.9.3"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.build.json",
|
|
44
|
+
"test": "node --test src/*.test.ts",
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
46
|
+
}
|
|
47
|
+
}
|