@fickydev/pigent 0.1.15 → 0.1.18
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/CHANGELOG.md +18 -1
- package/README.md +184 -233
- package/TODO.md +4 -3
- package/drizzle/migrations/0006_flippant_bruce_banner.sql +1 -0
- package/drizzle/migrations/meta/0006_snapshot.json +711 -0
- package/drizzle/migrations/meta/_journal.json +7 -0
- package/package.json +1 -1
- package/src/agents/AgentRegistry.ts +4 -4
- package/src/agents/BotCommandHandler.ts +2 -2
- package/src/config/loadConfig.ts +5 -3
- package/src/config/schemas.ts +6 -1
- package/src/daemon/Scheduler.ts +21 -0
- package/src/db/schema.ts +1 -0
- package/src/pi/PiAgentRunner.ts +21 -3
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { LoadedAgentConfig, LoadedConfig,
|
|
1
|
+
import type { LoadedAgentConfig, LoadedConfig, LoadedProfileConfig } from "../config/schemas";
|
|
2
2
|
import type { Repositories } from "../db/repositories";
|
|
3
3
|
|
|
4
4
|
export class AgentRegistry {
|
|
5
5
|
private readonly agentsById: Map<string, LoadedAgentConfig>;
|
|
6
|
-
private readonly profilesById: Map<string,
|
|
6
|
+
private readonly profilesById: Map<string, LoadedProfileConfig>;
|
|
7
7
|
|
|
8
8
|
constructor(
|
|
9
9
|
private readonly config: LoadedConfig,
|
|
@@ -23,7 +23,7 @@ export class AgentRegistry {
|
|
|
23
23
|
return [...this.agentsById.values()];
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
listProfiles():
|
|
26
|
+
listProfiles(): LoadedProfileConfig[] {
|
|
27
27
|
return [...this.profilesById.values()];
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -31,7 +31,7 @@ export class AgentRegistry {
|
|
|
31
31
|
return this.agentsById.get(id) ?? null;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
getProfile(id: string):
|
|
34
|
+
getProfile(id: string): LoadedProfileConfig | null {
|
|
35
35
|
return this.profilesById.get(id) ?? null;
|
|
36
36
|
}
|
|
37
37
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AuthStorage, ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import type { InboundMessage, InlineKeyboardButton } from "../channels/types";
|
|
3
|
-
import type { LoadedAgentConfig,
|
|
3
|
+
import type { LoadedAgentConfig, LoadedProfileConfig, ModelChoiceConfig } from "../config/schemas";
|
|
4
4
|
import type { AgentSessionRow } from "../db/schema";
|
|
5
5
|
import type { Repositories } from "../db/repositories";
|
|
6
6
|
import { PiAgentRunner, type PiContextUsage } from "../pi/PiAgentRunner";
|
|
@@ -169,7 +169,7 @@ export class BotCommandHandler {
|
|
|
169
169
|
].join("\n");
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
private async loadPiStatus(session: AgentSessionRow, agent: LoadedAgentConfig, profile:
|
|
172
|
+
private async loadPiStatus(session: AgentSessionRow, agent: LoadedAgentConfig, profile: LoadedProfileConfig | null) {
|
|
173
173
|
try {
|
|
174
174
|
return await this.piRunner.status({ agent, profile, session });
|
|
175
175
|
} catch {
|
package/src/config/loadConfig.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { readdir, readFile } from "node:fs/promises";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { join, resolve } from "node:path";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import YAML from "yaml";
|
|
5
5
|
import {
|
|
6
6
|
AgentConfigSchema,
|
|
7
7
|
type LoadedAgentConfig,
|
|
8
8
|
type LoadedConfig,
|
|
9
|
+
type LoadedProfileConfig,
|
|
9
10
|
ProfileConfigSchema,
|
|
10
11
|
RootConfigSchema,
|
|
11
12
|
} from "./schemas";
|
|
@@ -60,7 +61,7 @@ async function loadAgents(rootDir: string): Promise<LoadedAgentConfig[]> {
|
|
|
60
61
|
async function loadProfiles(rootDir: string) {
|
|
61
62
|
const profilesDir = join(rootDir, "profiles");
|
|
62
63
|
const entries = await readdir(profilesDir, { withFileTypes: true }).catch(() => []);
|
|
63
|
-
const profiles = [];
|
|
64
|
+
const profiles: LoadedProfileConfig[] = [];
|
|
64
65
|
|
|
65
66
|
for (const entry of entries) {
|
|
66
67
|
if (!entry.isFile()) continue;
|
|
@@ -68,7 +69,8 @@ async function loadProfiles(rootDir: string) {
|
|
|
68
69
|
|
|
69
70
|
const configPath = join(profilesDir, entry.name);
|
|
70
71
|
const parsed = await readYamlFile(configPath);
|
|
71
|
-
|
|
72
|
+
const profile = ProfileConfigSchema.parse(parsed);
|
|
73
|
+
profiles.push({ ...profile, baseDir: dirname(configPath) });
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
return profiles;
|
package/src/config/schemas.ts
CHANGED
|
@@ -18,6 +18,7 @@ export const TaskConfigSchema = z.object({
|
|
|
18
18
|
prompt: z.string().min(1).default("If no useful action is needed, reply exactly: NOOP."),
|
|
19
19
|
channel: z.string().default("telegram"),
|
|
20
20
|
chatId: z.string().optional(),
|
|
21
|
+
maxRunsPerHour: z.number().int().nonnegative().default(12),
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
export const SchedulerConfigSchema = z.object({
|
|
@@ -102,9 +103,13 @@ export type LoadedAgentConfig = AgentConfig & {
|
|
|
102
103
|
systemPrompt: string;
|
|
103
104
|
};
|
|
104
105
|
|
|
106
|
+
export type LoadedProfileConfig = ProfileConfig & {
|
|
107
|
+
baseDir: string;
|
|
108
|
+
};
|
|
109
|
+
|
|
105
110
|
export type LoadedConfig = {
|
|
106
111
|
agents: LoadedAgentConfig[];
|
|
107
|
-
profiles:
|
|
112
|
+
profiles: LoadedProfileConfig[];
|
|
108
113
|
telegramChats: TelegramChatConfig[];
|
|
109
114
|
modelChoices: ModelChoiceConfig[];
|
|
110
115
|
scheduler: SchedulerConfig;
|
package/src/daemon/Scheduler.ts
CHANGED
|
@@ -7,6 +7,8 @@ import type { AgentRunner } from "../agents/AgentRunner";
|
|
|
7
7
|
import type { ChannelAdapter } from "../channels/types";
|
|
8
8
|
import { isTaskDue } from "./taskDue";
|
|
9
9
|
|
|
10
|
+
const ONE_HOUR_MS = 3_600_000;
|
|
11
|
+
|
|
10
12
|
export class Scheduler {
|
|
11
13
|
private running = false;
|
|
12
14
|
private tickTimer: ReturnType<typeof setTimeout> | null = null;
|
|
@@ -70,6 +72,7 @@ export class Scheduler {
|
|
|
70
72
|
prompt: saved.prompt,
|
|
71
73
|
channel: saved.channel,
|
|
72
74
|
chatId: saved.chatId,
|
|
75
|
+
maxRunsPerHour: saved.maxRunsPerHour ?? 12,
|
|
73
76
|
});
|
|
74
77
|
|
|
75
78
|
if (!this.running && this.tasks.length === 1) {
|
|
@@ -108,6 +111,7 @@ export class Scheduler {
|
|
|
108
111
|
prompt: row.prompt,
|
|
109
112
|
channel: row.channel,
|
|
110
113
|
chatId: row.chatId,
|
|
114
|
+
maxRunsPerHour: row.maxRunsPerHour ?? 12,
|
|
111
115
|
}));
|
|
112
116
|
|
|
113
117
|
return [...configTasks, ...dbTaskConfigs];
|
|
@@ -129,6 +133,23 @@ export class Scheduler {
|
|
|
129
133
|
const lastRun = await this.repositories.taskRuns.findLatest(task.agent, task.id);
|
|
130
134
|
if (!isTaskDue(lastRun?.createdAt ?? null, task.intervalMs)) return;
|
|
131
135
|
|
|
136
|
+
// Enforce hourly rate limit
|
|
137
|
+
const maxPerHour = task.maxRunsPerHour ?? 12;
|
|
138
|
+
const recentCount = await this.repositories.taskRuns.countRecentNotified(
|
|
139
|
+
task.agent,
|
|
140
|
+
task.id,
|
|
141
|
+
Date.now() - ONE_HOUR_MS,
|
|
142
|
+
);
|
|
143
|
+
if (recentCount >= maxPerHour) {
|
|
144
|
+
logger.warn("task rate limited — max runs per hour reached", {
|
|
145
|
+
taskId: task.id,
|
|
146
|
+
agent: task.agent,
|
|
147
|
+
recentCount,
|
|
148
|
+
maxPerHour,
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
132
153
|
const agent = this.registry.getAgent(task.agent);
|
|
133
154
|
if (!agent) {
|
|
134
155
|
logger.warn("task references unknown agent, skipping", { taskId: task.id, agent: task.agent });
|
package/src/db/schema.ts
CHANGED
|
@@ -116,6 +116,7 @@ export const taskConfigs = sqliteTable("task_configs", {
|
|
|
116
116
|
channel: text("channel").notNull().default("telegram"),
|
|
117
117
|
chatId: text("chat_id").notNull(),
|
|
118
118
|
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
|
119
|
+
maxRunsPerHour: integer("max_runs_per_hour"),
|
|
119
120
|
createdAt: integer("created_at").notNull(),
|
|
120
121
|
updatedAt: integer("updated_at").notNull(),
|
|
121
122
|
});
|
package/src/pi/PiAgentRunner.ts
CHANGED
|
@@ -8,14 +8,14 @@ import {
|
|
|
8
8
|
} from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import { mkdir } from "node:fs/promises";
|
|
10
10
|
import { resolve } from "node:path";
|
|
11
|
-
import type { LoadedAgentConfig,
|
|
11
|
+
import type { LoadedAgentConfig, LoadedProfileConfig } from "../config/schemas";
|
|
12
12
|
import type { AgentSessionRow } from "../db/schema";
|
|
13
13
|
import { resolveModelSelection } from "./PiModelResolver";
|
|
14
14
|
import { loadOrCreatePiSession } from "./PiSessionFactory";
|
|
15
15
|
|
|
16
16
|
export type PiAgentRunInput = {
|
|
17
17
|
agent: LoadedAgentConfig;
|
|
18
|
-
profile:
|
|
18
|
+
profile: LoadedProfileConfig | null;
|
|
19
19
|
session: AgentSessionRow;
|
|
20
20
|
prompt: string;
|
|
21
21
|
};
|
|
@@ -80,11 +80,13 @@ export class PiAgentRunner {
|
|
|
80
80
|
compaction: { enabled: false },
|
|
81
81
|
});
|
|
82
82
|
const agentDir = getAgentDir();
|
|
83
|
+
const skillPaths = resolveSkillPaths(input.agent, input.profile);
|
|
83
84
|
const resourceLoader = new DefaultResourceLoader({
|
|
84
85
|
cwd: workspace,
|
|
85
86
|
agentDir,
|
|
86
87
|
settingsManager,
|
|
87
88
|
systemPromptOverride: () => systemPrompt,
|
|
89
|
+
additionalSkillPaths: skillPaths,
|
|
88
90
|
});
|
|
89
91
|
await resourceLoader.reload();
|
|
90
92
|
|
|
@@ -109,7 +111,23 @@ export class PiAgentRunner {
|
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
function
|
|
114
|
+
function resolveSkillPaths(agent: LoadedAgentConfig, profile: LoadedProfileConfig | null): string[] {
|
|
115
|
+
const paths: string[] = [];
|
|
116
|
+
|
|
117
|
+
if (profile) {
|
|
118
|
+
for (const s of profile.defaultSkills) {
|
|
119
|
+
paths.push(resolve(profile.baseDir, s));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (const s of agent.skills) {
|
|
124
|
+
paths.push(resolve(agent.baseDir, s));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return paths;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function composeSystemPrompt(agent: LoadedAgentConfig, profile: LoadedProfileConfig | null): string {
|
|
113
131
|
return [
|
|
114
132
|
profile?.instructions,
|
|
115
133
|
agent.systemPrompt,
|