@openshain/core 0.2.0 → 0.4.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/NOTICE +4 -0
- package/dist/authority/policy.d.ts +131 -0
- package/dist/authority/policy.js +335 -0
- package/dist/config/load.js +4 -41
- package/dist/config/schema.d.ts +12 -9
- package/dist/config/schema.js +14 -10
- package/dist/config/yaml.d.ts +12 -0
- package/dist/config/yaml.js +49 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -2
- package/dist/runtime.d.ts +28 -3
- package/dist/runtime.js +148 -6
- package/dist/schemas.d.ts +1 -1
- package/dist/schemas.js +3 -0
- package/dist/tool/ask-user.d.ts +5 -0
- package/dist/tool/ask-user.js +22 -0
- package/dist/tool/paths.d.ts +1 -1
- package/dist/tool/paths.js +1 -1
- package/dist/tool/types.js +6 -0
- package/dist/work/events.d.ts +150 -1
- package/dist/work/events.js +146 -0
- package/dist/work/history.d.ts +57 -0
- package/dist/work/history.js +81 -0
- package/dist/work/projection.js +21 -6
- package/package.json +3 -2
- package/src/authority/policy.ts +400 -0
- package/src/config/load.ts +4 -43
- package/src/config/schema.ts +41 -31
- package/src/config/yaml.ts +60 -0
- package/src/index.ts +43 -1
- package/src/runtime.ts +189 -10
- package/src/schemas.ts +17 -1
- package/src/tool/ask-user.ts +25 -0
- package/src/tool/paths.ts +1 -1
- package/src/tool/types.ts +6 -0
- package/src/work/events.ts +192 -0
- package/src/work/history.ts +121 -0
- package/src/work/projection.ts +21 -6
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { AnyEvent, Event } from "./events.ts";
|
|
2
|
+
|
|
3
|
+
/** Why a client gives up on a work, as recorded in `work.failed`. */
|
|
4
|
+
export type FailureReason = "limit_reached" | "model_refusal" | "model_error";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Counts the tool calls of a work the way the limits do: every call the runtime started, plus
|
|
8
|
+
* every rejection that never became a call. A rejection of a started call is not a second call.
|
|
9
|
+
*/
|
|
10
|
+
export function countToolCalls(events: readonly AnyEvent[]): number {
|
|
11
|
+
let count = 0;
|
|
12
|
+
let started = new Set<string>();
|
|
13
|
+
for (const event of events) {
|
|
14
|
+
if (event.type === "model.completed") started = new Set();
|
|
15
|
+
else if (event.type === "tool.called") {
|
|
16
|
+
started.add((event as Event<"tool.called">).payload.callId);
|
|
17
|
+
count += 1;
|
|
18
|
+
} else if (event.type === "tool.rejected") {
|
|
19
|
+
if (!started.has((event as Event<"tool.rejected">).payload.callId)) count += 1;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return count;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PendingQuestion {
|
|
26
|
+
callId: string;
|
|
27
|
+
question: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The questions of the work that have no answer yet, oldest first. Call ids of questions are
|
|
32
|
+
* minted by the runtime, so the whole log is searched: a client recording its own model turns
|
|
33
|
+
* must not hide a question.
|
|
34
|
+
*/
|
|
35
|
+
export function pendingQuestions(events: readonly AnyEvent[]): PendingQuestion[] {
|
|
36
|
+
const answered = new Set(
|
|
37
|
+
events
|
|
38
|
+
.filter((e): e is Event<"human.input_provided"> => e.type === "human.input_provided")
|
|
39
|
+
.map((e) => e.payload.callId),
|
|
40
|
+
);
|
|
41
|
+
return events
|
|
42
|
+
.filter((e): e is Event<"human.input_requested"> => e.type === "human.input_requested")
|
|
43
|
+
.filter((e) => !answered.has(e.payload.callId))
|
|
44
|
+
.map((e) => ({ callId: e.payload.callId, question: e.payload.question }));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PendingApproval {
|
|
48
|
+
approvalId: string;
|
|
49
|
+
call: { callId: string; name: string; input: unknown };
|
|
50
|
+
ruleId: string;
|
|
51
|
+
kind: "approval" | "review";
|
|
52
|
+
approvers?: string[];
|
|
53
|
+
reviewer?: { role: string; name?: string };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The approvals of the work that have no decision yet, oldest first. */
|
|
57
|
+
export function pendingApprovals(events: readonly AnyEvent[]): PendingApproval[] {
|
|
58
|
+
const decided = new Set(
|
|
59
|
+
events
|
|
60
|
+
.filter((e): e is Event<"approval.decided"> => e.type === "approval.decided")
|
|
61
|
+
.map((e) => e.payload.approvalId),
|
|
62
|
+
);
|
|
63
|
+
return events
|
|
64
|
+
.filter((e): e is Event<"approval.requested"> => e.type === "approval.requested")
|
|
65
|
+
.filter((e) => !decided.has(e.payload.approvalId))
|
|
66
|
+
.map((e) => ({ ...e.payload }));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface HistoryCall {
|
|
70
|
+
callId: string;
|
|
71
|
+
name: string;
|
|
72
|
+
/** The path the call named, when its input had one. */
|
|
73
|
+
path?: string;
|
|
74
|
+
/** Present once the call has a result; absent while it is still open. */
|
|
75
|
+
isError?: boolean;
|
|
76
|
+
rejected?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface WorkHistory {
|
|
80
|
+
calls: HistoryCall[];
|
|
81
|
+
/** Calls that were started but have no result: the work stopped while they ran. */
|
|
82
|
+
unfinished: HistoryCall[];
|
|
83
|
+
pending: PendingQuestion[];
|
|
84
|
+
/** Calls held for approval that nobody has decided on yet. */
|
|
85
|
+
approvals: PendingApproval[];
|
|
86
|
+
toolCalls: number;
|
|
87
|
+
/** Model calls recorded on the work, for a client that counts them against a limit. */
|
|
88
|
+
modelCalls: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** What a client needs to pick a work up where it stopped. Built from the log alone. */
|
|
92
|
+
export function workHistory(events: readonly AnyEvent[]): WorkHistory {
|
|
93
|
+
const calls: HistoryCall[] = [];
|
|
94
|
+
const byId = new Map<string, HistoryCall>();
|
|
95
|
+
for (const event of events) {
|
|
96
|
+
if (event.type === "tool.called") {
|
|
97
|
+
const { callId, name, input } = (event as Event<"tool.called">).payload;
|
|
98
|
+
const path = (input as { path?: unknown } | null)?.path;
|
|
99
|
+
const call: HistoryCall = { callId, name, ...(typeof path === "string" && { path }) };
|
|
100
|
+
calls.push(call);
|
|
101
|
+
byId.set(callId, call);
|
|
102
|
+
} else if (event.type === "tool.completed") {
|
|
103
|
+
const { callId, isError } = (event as Event<"tool.completed">).payload;
|
|
104
|
+
const call = byId.get(callId);
|
|
105
|
+
if (call) call.isError = isError;
|
|
106
|
+
} else if (event.type === "tool.rejected") {
|
|
107
|
+
const { callId, name, code } = (event as Event<"tool.rejected">).payload;
|
|
108
|
+
const call = byId.get(callId);
|
|
109
|
+
if (call) call.rejected = code;
|
|
110
|
+
else calls.push({ callId, name, rejected: code });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
calls,
|
|
115
|
+
unfinished: calls.filter((c) => c.isError === undefined && c.rejected === undefined),
|
|
116
|
+
pending: pendingQuestions(events),
|
|
117
|
+
approvals: pendingApprovals(events),
|
|
118
|
+
toolCalls: countToolCalls(events),
|
|
119
|
+
modelCalls: events.filter((e) => e.type === "model.requested").length,
|
|
120
|
+
};
|
|
121
|
+
}
|
package/src/work/projection.ts
CHANGED
|
@@ -35,12 +35,24 @@ export function buildProjection(input: ProjectionInput): Projection {
|
|
|
35
35
|
first?.type === "work.created" ? (first as Event<"work.created">).payload.agentName : undefined;
|
|
36
36
|
const system = [
|
|
37
37
|
config.profession.instructions.trim(),
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
[
|
|
39
|
+
"# 立場",
|
|
40
|
+
`この会社は ${config.company.name}。依頼する人は ${config.principal.name}(${config.principal.id})。あなたはこの人の代理として働き、この人と話す。あなた自身は ${config.principal.name} ではなく、この会社で働く社員エージェント。`,
|
|
41
|
+
...(agentName
|
|
42
|
+
? [
|
|
43
|
+
`あなたの名前は ${agentName}。名乗るときはこの名前と、社員エージェントであることを言う。`,
|
|
44
|
+
]
|
|
45
|
+
: []),
|
|
46
|
+
"",
|
|
47
|
+
"# 数字と事実",
|
|
48
|
+
"件数、合計、検索の結果は Tool が返した値をそのまま使う。自分で数え直したり足し直したりしない。日付と時刻は context を呼んで確かめ、推測しない。",
|
|
49
|
+
"",
|
|
50
|
+
"# 残り回数",
|
|
51
|
+
"各ターンの最後に「残り model 呼び出し N 回、Tool 呼び出し M 回」という 1 行が user message として届く。残量の通知なので、返事は要らない。",
|
|
52
|
+
"",
|
|
53
|
+
"# 終わり方",
|
|
54
|
+
"依頼が終わったら、何をしたかと結果の数字を書いて終える。",
|
|
55
|
+
].join("\n"),
|
|
44
56
|
].join("\n\n");
|
|
45
57
|
|
|
46
58
|
const messages: ModelMessage[] = [];
|
|
@@ -61,6 +73,9 @@ export function buildProjection(input: ProjectionInput): Projection {
|
|
|
61
73
|
case "human.message":
|
|
62
74
|
pushUserPart({ type: "text", text: (event as Event<"human.message">).payload.text });
|
|
63
75
|
break;
|
|
76
|
+
case "prompt.expanded":
|
|
77
|
+
pushUserPart({ type: "text", text: (event as Event<"prompt.expanded">).payload.text });
|
|
78
|
+
break;
|
|
64
79
|
case "model.completed": {
|
|
65
80
|
const content = (event as Event<"model.completed">).payload.content
|
|
66
81
|
.filter((part) => part.type !== "opaque" || part.provider === input.providerId)
|