@oyasmi/pipiclaw 0.6.10 → 0.7.0
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 +1 -1
- package/dist/agent/channel-runner.d.ts +12 -1
- package/dist/agent/channel-runner.js +58 -3
- package/dist/agent/commands.d.ts +1 -1
- package/dist/agent/commands.js +10 -0
- package/dist/agent/prompt-builder.d.ts +17 -0
- package/dist/agent/prompt-builder.js +42 -23
- package/dist/agent/session-events.d.ts +2 -0
- package/dist/agent/session-events.js +31 -12
- package/dist/agent/types.d.ts +9 -0
- package/dist/agent/types.js +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/log.d.ts +24 -0
- package/dist/log.js +96 -1
- package/dist/memory/bootstrap.js +35 -1
- package/dist/memory/candidates.js +21 -3
- package/dist/memory/consolidation.d.ts +7 -1
- package/dist/memory/consolidation.js +107 -36
- package/dist/memory/files.d.ts +40 -0
- package/dist/memory/files.js +0 -0
- package/dist/memory/lifecycle.js +2 -0
- package/dist/memory/maintenance-jobs.js +10 -4
- package/dist/memory/post-turn-review.js +4 -2
- package/dist/memory/promotion-signals.js +0 -1
- package/dist/memory/recall.d.ts +1 -0
- package/dist/memory/recall.js +11 -2
- package/dist/memory/session-search.d.ts +1 -0
- package/dist/memory/session-search.js +1 -0
- package/dist/memory/session.d.ts +1 -0
- package/dist/memory/session.js +44 -13
- package/dist/memory/sidecar-worker.d.ts +4 -0
- package/dist/memory/sidecar-worker.js +29 -0
- package/dist/memory/transcript.d.ts +9 -0
- package/dist/memory/transcript.js +30 -0
- package/dist/paths.d.ts +3 -0
- package/dist/paths.js +3 -0
- package/dist/runtime/bootstrap.js +151 -15
- package/dist/runtime/delivery.js +17 -0
- package/dist/runtime/dingtalk.d.ts +2 -0
- package/dist/runtime/dingtalk.js +19 -4
- package/dist/sandbox.d.ts +8 -0
- package/dist/sandbox.js +21 -17
- package/dist/settings.d.ts +16 -0
- package/dist/settings.js +22 -0
- package/dist/shared/jsonl-appender.d.ts +25 -0
- package/dist/shared/jsonl-appender.js +85 -0
- package/dist/subagents/tool.d.ts +2 -2
- package/dist/subagents/tool.js +25 -56
- package/dist/tools/bash.d.ts +8 -1
- package/dist/tools/bash.js +46 -21
- package/dist/tools/config.d.ts +3 -0
- package/dist/tools/config.js +9 -0
- package/dist/tools/edit.d.ts +1 -0
- package/dist/tools/edit.js +18 -11
- package/dist/tools/index.js +18 -53
- package/dist/tools/memory-save.d.ts +17 -0
- package/dist/tools/memory-save.js +43 -0
- package/dist/tools/read.d.ts +2 -2
- package/dist/tools/read.js +2 -2
- package/dist/tools/registry.d.ts +63 -0
- package/dist/tools/registry.js +154 -0
- package/dist/tools/session-search.d.ts +3 -2
- package/dist/tools/session-search.js +10 -5
- package/dist/tools/skill-list.js +1 -1
- package/dist/tools/skill-manage.d.ts +1 -1
- package/dist/tools/skill-manage.js +3 -1
- package/dist/tools/skill-view.js +12 -11
- package/dist/tools/web-search.d.ts +1 -1
- package/dist/tools/web-search.js +1 -1
- package/dist/usage/ledger.d.ts +47 -0
- package/dist/usage/ledger.js +95 -0
- package/dist/usage/render.d.ts +11 -0
- package/dist/usage/render.js +67 -0
- package/package.json +5 -5
- package/dist/tools/attach.d.ts +0 -20
- package/dist/tools/attach.js +0 -69
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type UsageKind = "turn" | "subagent" | "sidecar";
|
|
2
|
+
export interface UsageTokens {
|
|
3
|
+
input: number;
|
|
4
|
+
output: number;
|
|
5
|
+
cacheRead: number;
|
|
6
|
+
cacheWrite: number;
|
|
7
|
+
total: number;
|
|
8
|
+
}
|
|
9
|
+
export interface UsageCost {
|
|
10
|
+
input: number;
|
|
11
|
+
output: number;
|
|
12
|
+
cacheRead: number;
|
|
13
|
+
cacheWrite: number;
|
|
14
|
+
total: number;
|
|
15
|
+
}
|
|
16
|
+
export interface UsageLedgerEntry {
|
|
17
|
+
ts: string;
|
|
18
|
+
channelId: string;
|
|
19
|
+
kind: UsageKind;
|
|
20
|
+
model: string;
|
|
21
|
+
label?: string;
|
|
22
|
+
usage: UsageTokens;
|
|
23
|
+
cost: UsageCost;
|
|
24
|
+
}
|
|
25
|
+
export interface UsageSummary {
|
|
26
|
+
totalCost: number;
|
|
27
|
+
entryCount: number;
|
|
28
|
+
byKind: Record<string, number>;
|
|
29
|
+
byModel: Record<string, number>;
|
|
30
|
+
byChannel: Record<string, number>;
|
|
31
|
+
}
|
|
32
|
+
export interface UsageSummaryQuery {
|
|
33
|
+
since: Date;
|
|
34
|
+
until?: Date;
|
|
35
|
+
channelId?: string;
|
|
36
|
+
}
|
|
37
|
+
export declare const UNTRACKED_CHANNEL_ID = "(untracked)";
|
|
38
|
+
export interface UsageLedger {
|
|
39
|
+
record(entry: Omit<UsageLedgerEntry, "ts">): void;
|
|
40
|
+
summarize(query: UsageSummaryQuery): UsageSummary;
|
|
41
|
+
}
|
|
42
|
+
export interface CreateUsageLedgerOptions {
|
|
43
|
+
baseDir?: string;
|
|
44
|
+
}
|
|
45
|
+
export declare function createUsageLedger(options?: CreateUsageLedgerOptions): UsageLedger;
|
|
46
|
+
/** Process-wide ledger (single-process runtime). Tests use createUsageLedger. */
|
|
47
|
+
export declare function getUsageLedger(): UsageLedger;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import * as log from "../log.js";
|
|
4
|
+
import { USAGE_STATE_DIR } from "../paths.js";
|
|
5
|
+
import { createJsonlAppender } from "../shared/jsonl-appender.js";
|
|
6
|
+
export const UNTRACKED_CHANNEL_ID = "(untracked)";
|
|
7
|
+
function monthKey(date) {
|
|
8
|
+
return `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, "0")}`;
|
|
9
|
+
}
|
|
10
|
+
function monthKeysBetween(since, until) {
|
|
11
|
+
const keys = [];
|
|
12
|
+
const cursor = new Date(Date.UTC(since.getUTCFullYear(), since.getUTCMonth(), 1));
|
|
13
|
+
const end = new Date(Date.UTC(until.getUTCFullYear(), until.getUTCMonth(), 1));
|
|
14
|
+
while (cursor <= end) {
|
|
15
|
+
keys.push(monthKey(cursor));
|
|
16
|
+
cursor.setUTCMonth(cursor.getUTCMonth() + 1);
|
|
17
|
+
}
|
|
18
|
+
return keys;
|
|
19
|
+
}
|
|
20
|
+
export function createUsageLedger(options = {}) {
|
|
21
|
+
const baseDir = options.baseDir ?? USAGE_STATE_DIR;
|
|
22
|
+
const fileFor = (date) => join(baseDir, `usage-${monthKey(date)}.jsonl`);
|
|
23
|
+
const appender = createJsonlAppender({ pathFor: fileFor });
|
|
24
|
+
return {
|
|
25
|
+
record(entry) {
|
|
26
|
+
// No API billing (local models) → no ledger noise.
|
|
27
|
+
if (!(entry.cost.total > 0))
|
|
28
|
+
return;
|
|
29
|
+
if (!entry.channelId) {
|
|
30
|
+
log.logWarning("Usage ledger entry missing channelId; recording as untracked", `kind=${entry.kind}`);
|
|
31
|
+
}
|
|
32
|
+
const full = {
|
|
33
|
+
ts: new Date().toISOString(),
|
|
34
|
+
...entry,
|
|
35
|
+
channelId: entry.channelId || UNTRACKED_CHANNEL_ID,
|
|
36
|
+
};
|
|
37
|
+
void appender.append(full);
|
|
38
|
+
},
|
|
39
|
+
summarize(query) {
|
|
40
|
+
const until = query.until ?? new Date();
|
|
41
|
+
const sinceMs = query.since.getTime();
|
|
42
|
+
const untilMs = until.getTime();
|
|
43
|
+
const summary = {
|
|
44
|
+
totalCost: 0,
|
|
45
|
+
entryCount: 0,
|
|
46
|
+
byKind: {},
|
|
47
|
+
byModel: {},
|
|
48
|
+
byChannel: {},
|
|
49
|
+
};
|
|
50
|
+
for (const key of monthKeysBetween(query.since, until)) {
|
|
51
|
+
const path = join(baseDir, `usage-${key}.jsonl`);
|
|
52
|
+
if (!existsSync(path))
|
|
53
|
+
continue;
|
|
54
|
+
let content;
|
|
55
|
+
try {
|
|
56
|
+
content = readFileSync(path, "utf-8");
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const line of content.split("\n")) {
|
|
62
|
+
if (!line)
|
|
63
|
+
continue;
|
|
64
|
+
let entry;
|
|
65
|
+
try {
|
|
66
|
+
entry = JSON.parse(line);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
continue; // tolerate a torn trailing line
|
|
70
|
+
}
|
|
71
|
+
const tsMs = Date.parse(entry.ts);
|
|
72
|
+
if (Number.isNaN(tsMs) || tsMs < sinceMs || tsMs > untilMs)
|
|
73
|
+
continue;
|
|
74
|
+
if (query.channelId && entry.channelId !== query.channelId)
|
|
75
|
+
continue;
|
|
76
|
+
const cost = entry.cost?.total ?? 0;
|
|
77
|
+
summary.totalCost += cost;
|
|
78
|
+
summary.entryCount += 1;
|
|
79
|
+
summary.byKind[entry.kind] = (summary.byKind[entry.kind] ?? 0) + cost;
|
|
80
|
+
summary.byModel[entry.model] = (summary.byModel[entry.model] ?? 0) + cost;
|
|
81
|
+
summary.byChannel[entry.channelId] = (summary.byChannel[entry.channelId] ?? 0) + cost;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return summary;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
let singleton = null;
|
|
89
|
+
/** Process-wide ledger (single-process runtime). Tests use createUsageLedger. */
|
|
90
|
+
export function getUsageLedger() {
|
|
91
|
+
if (!singleton) {
|
|
92
|
+
singleton = createUsageLedger();
|
|
93
|
+
}
|
|
94
|
+
return singleton;
|
|
95
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { UsageLedger } from "./ledger.js";
|
|
2
|
+
export type UsageQueryMode = "default" | "7d" | "month";
|
|
3
|
+
export declare function parseUsageMode(args: string): UsageQueryMode;
|
|
4
|
+
interface UsageWindow {
|
|
5
|
+
title: string;
|
|
6
|
+
since: Date;
|
|
7
|
+
until: Date;
|
|
8
|
+
}
|
|
9
|
+
export declare function usageWindows(mode: UsageQueryMode, now: Date): UsageWindow[];
|
|
10
|
+
export declare function renderUsageReport(ledger: UsageLedger, channelId: string, mode: UsageQueryMode, now: Date): string;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export function parseUsageMode(args) {
|
|
2
|
+
const normalized = args.trim().toLowerCase();
|
|
3
|
+
if (normalized === "7d" || normalized === "7")
|
|
4
|
+
return "7d";
|
|
5
|
+
if (normalized === "month" || normalized === "monthly")
|
|
6
|
+
return "month";
|
|
7
|
+
return "default";
|
|
8
|
+
}
|
|
9
|
+
function startOfUtcDay(now) {
|
|
10
|
+
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
|
11
|
+
}
|
|
12
|
+
function startOfUtcMonth(now) {
|
|
13
|
+
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
|
14
|
+
}
|
|
15
|
+
export function usageWindows(mode, now) {
|
|
16
|
+
const monthTitle = `This month (${now.getUTCFullYear()}-${String(now.getUTCMonth() + 1).padStart(2, "0")})`;
|
|
17
|
+
switch (mode) {
|
|
18
|
+
case "7d":
|
|
19
|
+
return [{ title: "Last 7 days", since: new Date(now.getTime() - 7 * 86_400_000), until: now }];
|
|
20
|
+
case "month":
|
|
21
|
+
return [{ title: monthTitle, since: startOfUtcMonth(now), until: now }];
|
|
22
|
+
default:
|
|
23
|
+
return [
|
|
24
|
+
{ title: "Today (UTC)", since: startOfUtcDay(now), until: now },
|
|
25
|
+
{ title: monthTitle, since: startOfUtcMonth(now), until: now },
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function money(n) {
|
|
30
|
+
return `$${n.toFixed(4)}`;
|
|
31
|
+
}
|
|
32
|
+
function topEntries(map, limit) {
|
|
33
|
+
return Object.entries(map)
|
|
34
|
+
.filter(([, cost]) => cost > 0)
|
|
35
|
+
.sort((a, b) => b[1] - a[1])
|
|
36
|
+
.slice(0, limit);
|
|
37
|
+
}
|
|
38
|
+
function renderKindBreakdown(summary) {
|
|
39
|
+
const parts = topEntries(summary.byKind, 3).map(([kind, cost]) => `${kind} ${money(cost)}`);
|
|
40
|
+
return parts.length > 0 ? parts.join(" · ") : null;
|
|
41
|
+
}
|
|
42
|
+
function renderWindow(window, ledger, channelId) {
|
|
43
|
+
const global = ledger.summarize({ since: window.since, until: window.until });
|
|
44
|
+
const channel = ledger.summarize({ since: window.since, until: window.until, channelId });
|
|
45
|
+
const lines = [`## ${window.title}`];
|
|
46
|
+
if (global.entryCount === 0) {
|
|
47
|
+
lines.push("No recorded usage.");
|
|
48
|
+
return lines.join("\n");
|
|
49
|
+
}
|
|
50
|
+
lines.push(`This channel: ${money(channel.totalCost)}`);
|
|
51
|
+
const channelKinds = renderKindBreakdown(channel);
|
|
52
|
+
if (channelKinds) {
|
|
53
|
+
lines.push(` ${channelKinds}`);
|
|
54
|
+
}
|
|
55
|
+
const channelCount = Object.keys(global.byChannel).length;
|
|
56
|
+
lines.push(`Global: ${money(global.totalCost)} across ${channelCount} channel${channelCount === 1 ? "" : "s"}`);
|
|
57
|
+
const topModels = topEntries(global.byModel, 3);
|
|
58
|
+
if (topModels.length > 0) {
|
|
59
|
+
lines.push(` Top models: ${topModels.map(([model, cost]) => `${model} ${money(cost)}`).join(", ")}`);
|
|
60
|
+
}
|
|
61
|
+
return lines.join("\n");
|
|
62
|
+
}
|
|
63
|
+
export function renderUsageReport(ledger, channelId, mode, now) {
|
|
64
|
+
const windows = usageWindows(mode, now);
|
|
65
|
+
const body = windows.map((window) => renderWindow(window, ledger, channelId)).join("\n\n");
|
|
66
|
+
return `# Usage\n\n${body}`;
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oyasmi/pipiclaw",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "An AI assistant runtime for coding and team workflows, with DingTalk AI Cards, sub-agents, memory, and scheduled events.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -34,14 +34,14 @@
|
|
|
34
34
|
"prepublishOnly": "npm run clean && npm run build && npm run check"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@earendil-works/pi-agent-core": "0.80.
|
|
38
|
-
"@earendil-works/pi-ai": "0.80.
|
|
39
|
-
"@earendil-works/pi-coding-agent": "0.80.
|
|
37
|
+
"@earendil-works/pi-agent-core": "0.80.3",
|
|
38
|
+
"@earendil-works/pi-ai": "0.80.3",
|
|
39
|
+
"@earendil-works/pi-coding-agent": "0.80.3",
|
|
40
40
|
"@mozilla/readability": "^0.6.0",
|
|
41
41
|
"axios": "^1.7.0",
|
|
42
42
|
"croner": "^9.1.0",
|
|
43
43
|
"diff": "^8.0.2",
|
|
44
|
-
"dingtalk-stream": "
|
|
44
|
+
"dingtalk-stream": "2.1.5",
|
|
45
45
|
"http-proxy-agent": "^7.0.2",
|
|
46
46
|
"https-proxy-agent": "^7.0.6",
|
|
47
47
|
"jsdom": "^26.1.0",
|
package/dist/tools/attach.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
2
|
-
import { Type } from "typebox";
|
|
3
|
-
import type { SecurityConfig, SecurityRuntimeContext } from "../security/types.js";
|
|
4
|
-
declare const attachSchema: Type.TObject<{
|
|
5
|
-
label: Type.TString;
|
|
6
|
-
path: Type.TString;
|
|
7
|
-
title: Type.TOptional<Type.TString>;
|
|
8
|
-
}>;
|
|
9
|
-
export type UploadFunction = (filePath: string, title?: string) => Promise<void>;
|
|
10
|
-
export interface AttachToolOptions {
|
|
11
|
-
securityConfig?: SecurityConfig;
|
|
12
|
-
securityContext?: SecurityRuntimeContext;
|
|
13
|
-
channelId?: string;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Create the attach tool. If no uploadFn is provided, the tool will throw
|
|
17
|
-
* an informative error guiding the LLM to use alternative approaches.
|
|
18
|
-
*/
|
|
19
|
-
export declare function createAttachTool(uploadFn?: UploadFunction, options?: AttachToolOptions): AgentTool<typeof attachSchema>;
|
|
20
|
-
export {};
|
package/dist/tools/attach.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { basename, resolve as resolvePath } from "path";
|
|
2
|
-
import { Type } from "typebox";
|
|
3
|
-
import { DEFAULT_SECURITY_CONFIG } from "../security/config.js";
|
|
4
|
-
import { logSecurityEvent } from "../security/logger.js";
|
|
5
|
-
import { guardPath } from "../security/path-guard.js";
|
|
6
|
-
const attachSchema = Type.Object({
|
|
7
|
-
label: Type.String({ description: "Brief description of what you're sharing (shown to user)" }),
|
|
8
|
-
path: Type.String({ description: "Path to the file to attach" }),
|
|
9
|
-
title: Type.Optional(Type.String({ description: "Title for the file (defaults to filename)" })),
|
|
10
|
-
});
|
|
11
|
-
/**
|
|
12
|
-
* Create the attach tool. If no uploadFn is provided, the tool will throw
|
|
13
|
-
* an informative error guiding the LLM to use alternative approaches.
|
|
14
|
-
*/
|
|
15
|
-
export function createAttachTool(uploadFn, options = {}) {
|
|
16
|
-
const securityConfig = options.securityConfig ?? DEFAULT_SECURITY_CONFIG;
|
|
17
|
-
const securityContext = options.securityContext ?? {
|
|
18
|
-
workspaceDir: process.cwd(),
|
|
19
|
-
workspacePath: process.cwd(),
|
|
20
|
-
cwd: process.cwd(),
|
|
21
|
-
};
|
|
22
|
-
return {
|
|
23
|
-
name: "attach",
|
|
24
|
-
label: "attach",
|
|
25
|
-
description: "Attach a file to your response. Use this to share files, images, or documents with the user. Only files from /workspace/ can be attached.",
|
|
26
|
-
parameters: attachSchema,
|
|
27
|
-
execute: async (_toolCallId, { path, title }, signal) => {
|
|
28
|
-
if (!uploadFn) {
|
|
29
|
-
throw new Error("File upload is not supported in DingTalk mode. Output file content as text instead, or use bash to host files.");
|
|
30
|
-
}
|
|
31
|
-
if (signal?.aborted) {
|
|
32
|
-
throw new Error("Operation aborted");
|
|
33
|
-
}
|
|
34
|
-
const absolutePath = resolvePath(path);
|
|
35
|
-
if (securityConfig.enabled && securityConfig.pathGuard.enabled) {
|
|
36
|
-
const readGuard = guardPath(path, "read", { ...securityContext, config: securityConfig.pathGuard });
|
|
37
|
-
if (!readGuard.allowed) {
|
|
38
|
-
logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
39
|
-
type: "path",
|
|
40
|
-
tool: "attach",
|
|
41
|
-
channelId: options.channelId,
|
|
42
|
-
rawPath: path,
|
|
43
|
-
operation: "read",
|
|
44
|
-
resolvedPath: readGuard.resolvedPath,
|
|
45
|
-
category: readGuard.category,
|
|
46
|
-
reason: readGuard.reason,
|
|
47
|
-
});
|
|
48
|
-
throw new Error([
|
|
49
|
-
`Path blocked${readGuard.category ? ` [${readGuard.category}]` : ""}`,
|
|
50
|
-
readGuard.reason ? `Reason: ${readGuard.reason}` : "",
|
|
51
|
-
readGuard.resolvedPath ? `Resolved path: ${readGuard.resolvedPath}` : "",
|
|
52
|
-
]
|
|
53
|
-
.filter(Boolean)
|
|
54
|
-
.join("\n"));
|
|
55
|
-
}
|
|
56
|
-
const workspaceRoot = resolvePath(securityContext.workspaceDir);
|
|
57
|
-
if (absolutePath !== workspaceRoot && !absolutePath.startsWith(`${workspaceRoot}/`)) {
|
|
58
|
-
throw new Error("Attach is limited to files inside the workspace directory.");
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const fileName = title || basename(absolutePath);
|
|
62
|
-
await uploadFn(absolutePath, fileName);
|
|
63
|
-
return {
|
|
64
|
-
content: [{ type: "text", text: `Attached file: ${fileName}` }],
|
|
65
|
-
details: undefined,
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|