fix(khala): address review #132 — reuse nerve-core Result, RETURNING for appendMessage, configurable timeout
- Remove duplicate result.ts, import Result/ok/err from @uncaged/nerve-core - appendMessage uses INSERT...RETURNING instead of INSERT+SELECT - CloudRole.timeoutSeconds: per-role timeout (defaults to 300s) - TODO comments for rate limiting and capacity sensing
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
"check": "biome check ."
|
"check": "biome check ."
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@uncaged/nerve-core": "workspace:*",
|
||||||
"hono": "^4.7.0",
|
"hono": "^4.7.0",
|
||||||
"jsonata": "^2.0.5",
|
"jsonata": "^2.0.5",
|
||||||
"ulidx": "^2.4.1"
|
"ulidx": "^2.4.1"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect } from "vitest";
|
import { err, ok } from "@uncaged/nerve-core";
|
||||||
import { ok, err } from "../result.js";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
describe("Result", () => {
|
describe("Result", () => {
|
||||||
it("ok wraps a value", () => {
|
it("ok wraps a value", () => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
import type { Result } from "@uncaged/nerve-core";
|
||||||
|
import { err, ok } from "@uncaged/nerve-core";
|
||||||
import { type ULID, monotonicFactory } from "ulidx";
|
import { type ULID, monotonicFactory } from "ulidx";
|
||||||
import type { Result } from "./result.js";
|
|
||||||
import { err, ok } from "./result.js";
|
|
||||||
import type { Agent, GetThreadMessagesOpts, Message, Task, Thread } from "./types.js";
|
import type { Agent, GetThreadMessagesOpts, Message, Task, Thread } from "./types.js";
|
||||||
|
|
||||||
const generateUlid = monotonicFactory((): number => Date.now());
|
const generateUlid = monotonicFactory((): number => Date.now());
|
||||||
@@ -88,19 +88,16 @@ export async function appendMessage(
|
|||||||
step: number,
|
step: number,
|
||||||
agentId: string | null,
|
agentId: string | null,
|
||||||
): Promise<Message> {
|
): Promise<Message> {
|
||||||
await db
|
const row = await db
|
||||||
.prepare(
|
.prepare(
|
||||||
`INSERT INTO messages (thread_id, role, content, meta, step, agent_id, created_at)
|
`INSERT INTO messages (thread_id, role, content, meta, step, agent_id, created_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))`,
|
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))
|
||||||
|
RETURNING *`,
|
||||||
)
|
)
|
||||||
.bind(threadId, role, content, meta, step, agentId)
|
.bind(threadId, role, content, meta, step, agentId)
|
||||||
.run();
|
|
||||||
const row = await db
|
|
||||||
.prepare("SELECT * FROM messages WHERE thread_id = ? AND step = ? ORDER BY id DESC LIMIT 1")
|
|
||||||
.bind(threadId, step)
|
|
||||||
.first<MessageRow>();
|
.first<MessageRow>();
|
||||||
if (!row) {
|
if (!row) {
|
||||||
throw new Error("appendMessage: row not found after insert");
|
throw new Error("appendMessage: INSERT RETURNING returned no row");
|
||||||
}
|
}
|
||||||
return rowToMessage(row);
|
return rowToMessage(row);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ app.get("/tasks", agentAuth, async (c) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Claim a task
|
// Claim a task
|
||||||
|
// TODO(#132): Add max_concurrent_claims per agent to prevent greedy claiming
|
||||||
app.post("/tasks/:id/claim", agentAuth, async (c) => {
|
app.post("/tasks/:id/claim", agentAuth, async (c) => {
|
||||||
const taskId = c.req.param("id");
|
const taskId = c.req.param("id");
|
||||||
const agentId = c.get("agentId");
|
const agentId = c.get("agentId");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
import type { Result } from "@uncaged/nerve-core";
|
||||||
|
import { err, ok } from "@uncaged/nerve-core";
|
||||||
import jsonata from "jsonata";
|
import jsonata from "jsonata";
|
||||||
import type { Result } from "./result.js";
|
|
||||||
import { err, ok } from "./result.js";
|
|
||||||
|
|
||||||
export type ModeratorDecision = { role: string };
|
export type ModeratorDecision = { role: string };
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
export type Result<T, E = string> = { ok: true; value: T } | { ok: false; error: E };
|
|
||||||
|
|
||||||
export function ok<T>(value: T): Result<T, never> {
|
|
||||||
return { ok: true, value };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function err<E = string>(error: E): Result<never, E> {
|
|
||||||
return { ok: false, error };
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
|
import type { Result } from "@uncaged/nerve-core";
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { requireAdmin, sha256Hex } from "../auth.js";
|
import { requireAdmin, sha256Hex } from "../auth.js";
|
||||||
import { deleteAgent, listAgents, registerAgent } from "../db.js";
|
import { deleteAgent, listAgents, registerAgent } from "../db.js";
|
||||||
import type { KhalaBindings } from "../env.js";
|
import type { KhalaBindings } from "../env.js";
|
||||||
import type { Result } from "../result.js";
|
|
||||||
|
|
||||||
const admin = new Hono<{ Bindings: KhalaBindings }>();
|
const admin = new Hono<{ Bindings: KhalaBindings }>();
|
||||||
|
|
||||||
@@ -21,6 +21,7 @@ admin.get("/agents", async (c) => {
|
|||||||
return c.json({ agents: [...agents] });
|
return c.json({ agents: [...agents] });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO(#132): Add rate limiting for agent registration endpoint
|
||||||
admin.post("/agents", async (c) => {
|
admin.post("/agents", async (c) => {
|
||||||
if (!requireAdmin(c)) {
|
if (!requireAdmin(c)) {
|
||||||
return c.json({ error: "unauthorized" }, 401);
|
return c.json({ error: "unauthorized" }, 401);
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export class ThreadDO {
|
|||||||
);
|
);
|
||||||
return { ok: true };
|
return { ok: true };
|
||||||
}
|
}
|
||||||
await createTask(db, threadId, role, r.prompt, 300);
|
await createTask(db, threadId, role, r.prompt, r.timeoutSeconds ?? 300);
|
||||||
return { ok: true };
|
return { ok: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
export type CloudRole = {
|
export type CloudRole = {
|
||||||
prompt: string;
|
prompt: string;
|
||||||
|
/** Turn timeout in seconds. Defaults to 300. */
|
||||||
|
timeoutSeconds: number | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CloudWorkflowDef = {
|
export type CloudWorkflowDef = {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { type CloudWorkflowDef, registerWorkflow } from "../workflows.js";
|
|||||||
const pingPong: CloudWorkflowDef = {
|
const pingPong: CloudWorkflowDef = {
|
||||||
name: "ping-pong",
|
name: "ping-pong",
|
||||||
roles: {
|
roles: {
|
||||||
pinger: { prompt: "Send the literal word ping" },
|
pinger: { prompt: "Send the literal word ping", timeoutSeconds: null },
|
||||||
ponger: { prompt: "Send the literal word pong" },
|
ponger: { prompt: "Send the literal word pong", timeoutSeconds: null },
|
||||||
},
|
},
|
||||||
moderator: `$count(steps) >= 6 ? { "role": "__end__" } : $count(steps) % 2 = 0 ? { "role": "pinger" } : { "role": "ponger" }`,
|
moderator: `$count(steps) >= 6 ? { "role": "__end__" } : $count(steps) % 2 = 0 ? { "role": "pinger" } : { "role": "ponger" }`,
|
||||||
};
|
};
|
||||||
|
|||||||
Generated
+3
@@ -86,6 +86,9 @@ importers:
|
|||||||
|
|
||||||
packages/khala:
|
packages/khala:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
'@uncaged/nerve-core':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../core
|
||||||
hono:
|
hono:
|
||||||
specifier: ^4.7.0
|
specifier: ^4.7.0
|
||||||
version: 4.12.15
|
version: 4.12.15
|
||||||
|
|||||||
Reference in New Issue
Block a user