@agentfield/sdk 0.1.139 → 0.1.140-rc.2
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/dist/index.js +173 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -911,6 +911,161 @@ var opencode_exports = {};
|
|
|
911
911
|
__export(opencode_exports, {
|
|
912
912
|
OpenCodeProvider: () => OpenCodeProvider
|
|
913
913
|
});
|
|
914
|
+
function openCodePermissions() {
|
|
915
|
+
return {
|
|
916
|
+
"*": "allow",
|
|
917
|
+
skill: { "agentfield*": "deny" },
|
|
918
|
+
question: "deny",
|
|
919
|
+
task: "deny"
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
function agentSystemPrompt(options) {
|
|
923
|
+
const systemPrompt = options.systemPrompt ?? options.system_prompt;
|
|
924
|
+
const callerPrompt = typeof systemPrompt === "string" ? systemPrompt.trim() : "";
|
|
925
|
+
return callerPrompt ? `${callerPrompt}
|
|
926
|
+
|
|
927
|
+
${agentFieldWorkerInstruction}` : agentFieldWorkerInstruction;
|
|
928
|
+
}
|
|
929
|
+
function optionEnvValue(options, name) {
|
|
930
|
+
const env = options.env;
|
|
931
|
+
if (typeof env === "object" && env !== null && Object.prototype.hasOwnProperty.call(env, name)) {
|
|
932
|
+
const value = env[name];
|
|
933
|
+
return typeof value === "string" ? value : void 0;
|
|
934
|
+
}
|
|
935
|
+
return process.env[name];
|
|
936
|
+
}
|
|
937
|
+
function inlineSystemPromptEnabled(options) {
|
|
938
|
+
const value = optionEnvValue(options, openCodeInlineSystemPromptEnv);
|
|
939
|
+
return typeof value === "string" && trueEnvValues.has(value.trim().toLowerCase());
|
|
940
|
+
}
|
|
941
|
+
function openCodeSteps(options) {
|
|
942
|
+
const value = optionEnvValue(options, openCodeStepsEnv);
|
|
943
|
+
if (typeof value === "string" && /^\+?\d+$/.test(value.trim())) {
|
|
944
|
+
const steps = Number(value.trim());
|
|
945
|
+
if (Number.isSafeInteger(steps) && steps > 0) {
|
|
946
|
+
return steps;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return openCodeDefaultSteps;
|
|
950
|
+
}
|
|
951
|
+
function isConfigObject(value) {
|
|
952
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
953
|
+
}
|
|
954
|
+
function deepMergeConfig(base, overlay) {
|
|
955
|
+
const merged = { ...base };
|
|
956
|
+
for (const [key, value] of Object.entries(overlay)) {
|
|
957
|
+
const existing = merged[key];
|
|
958
|
+
merged[key] = isConfigObject(existing) && isConfigObject(value) ? deepMergeConfig(existing, value) : value;
|
|
959
|
+
}
|
|
960
|
+
return merged;
|
|
961
|
+
}
|
|
962
|
+
function normalizeAgentFieldHarnessConfig(config, stripSystemPrompt) {
|
|
963
|
+
const agents = config.agent;
|
|
964
|
+
if (!isConfigObject(agents)) {
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
const selectedAgent = agents[openCodeAgentName];
|
|
968
|
+
if (!isConfigObject(selectedAgent)) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
if (stripSystemPrompt) {
|
|
972
|
+
delete selectedAgent.prompt;
|
|
973
|
+
}
|
|
974
|
+
const permission = selectedAgent.permission;
|
|
975
|
+
if (!isConfigObject(permission)) {
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const ordered = {};
|
|
979
|
+
if ("*" in permission) {
|
|
980
|
+
ordered["*"] = permission["*"];
|
|
981
|
+
}
|
|
982
|
+
for (const [key, value] of Object.entries(permission)) {
|
|
983
|
+
if (!["*", "skill", "question", "task"].includes(key)) {
|
|
984
|
+
ordered[key] = value;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
if ("skill" in permission) {
|
|
988
|
+
const skill = permission.skill;
|
|
989
|
+
if (isConfigObject(skill)) {
|
|
990
|
+
const orderedSkill = {};
|
|
991
|
+
for (const [key, value] of Object.entries(skill)) {
|
|
992
|
+
if (key !== "agentfield*") {
|
|
993
|
+
orderedSkill[key] = value;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
if ("agentfield*" in skill) {
|
|
997
|
+
orderedSkill["agentfield*"] = skill["agentfield*"];
|
|
998
|
+
}
|
|
999
|
+
ordered.skill = orderedSkill;
|
|
1000
|
+
} else {
|
|
1001
|
+
ordered.skill = skill;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
if ("question" in permission) {
|
|
1005
|
+
ordered.question = permission.question;
|
|
1006
|
+
}
|
|
1007
|
+
if ("task" in permission) {
|
|
1008
|
+
ordered.task = permission.task;
|
|
1009
|
+
}
|
|
1010
|
+
selectedAgent.permission = ordered;
|
|
1011
|
+
}
|
|
1012
|
+
function mergeOpenCodeConfigContent(existingContent, overlayContent, stripSystemPrompt = false) {
|
|
1013
|
+
if (!existingContent?.trim()) {
|
|
1014
|
+
return overlayContent;
|
|
1015
|
+
}
|
|
1016
|
+
let existing;
|
|
1017
|
+
let overlay;
|
|
1018
|
+
try {
|
|
1019
|
+
existing = JSON.parse(existingContent);
|
|
1020
|
+
overlay = JSON.parse(overlayContent);
|
|
1021
|
+
} catch (error) {
|
|
1022
|
+
throw new Error(
|
|
1023
|
+
"OPENCODE_CONFIG_CONTENT must be valid JSON when supplied to the AgentField OpenCode provider",
|
|
1024
|
+
{ cause: error }
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
if (!isConfigObject(existing)) {
|
|
1028
|
+
throw new Error(
|
|
1029
|
+
"OPENCODE_CONFIG_CONTENT must contain a JSON object when supplied to the AgentField OpenCode provider"
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
1032
|
+
if (!isConfigObject(overlay)) {
|
|
1033
|
+
throw new Error("AgentField OpenCode overlay must contain a JSON object");
|
|
1034
|
+
}
|
|
1035
|
+
const merged = deepMergeConfig(existing, overlay);
|
|
1036
|
+
normalizeAgentFieldHarnessConfig(merged, stripSystemPrompt);
|
|
1037
|
+
return JSON.stringify(merged);
|
|
1038
|
+
}
|
|
1039
|
+
function inlineOpenCodePrompt(prompt, options) {
|
|
1040
|
+
return `SYSTEM INSTRUCTIONS:
|
|
1041
|
+
${agentSystemPrompt(options)}
|
|
1042
|
+
|
|
1043
|
+
---
|
|
1044
|
+
|
|
1045
|
+
USER REQUEST:
|
|
1046
|
+
${prompt}`;
|
|
1047
|
+
}
|
|
1048
|
+
function buildOpenCodeConfigContent(options, modelValue, variantValue, includeSystemPrompt = true) {
|
|
1049
|
+
const agent = {
|
|
1050
|
+
mode: "primary",
|
|
1051
|
+
steps: openCodeSteps(options),
|
|
1052
|
+
permission: openCodePermissions()
|
|
1053
|
+
};
|
|
1054
|
+
if (includeSystemPrompt) {
|
|
1055
|
+
agent.prompt = agentSystemPrompt(options);
|
|
1056
|
+
}
|
|
1057
|
+
if (modelValue) {
|
|
1058
|
+
agent.model = modelValue;
|
|
1059
|
+
}
|
|
1060
|
+
if (variantValue) {
|
|
1061
|
+
agent.reasoningEffort = variantValue;
|
|
1062
|
+
}
|
|
1063
|
+
return JSON.stringify({
|
|
1064
|
+
$schema: openCodeConfigSchema,
|
|
1065
|
+
default_agent: openCodeAgentName,
|
|
1066
|
+
agent: { [openCodeAgentName]: agent }
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
914
1069
|
function extractOpenCodeError(stderr) {
|
|
915
1070
|
const lines = stderr.split(/\r?\n/);
|
|
916
1071
|
for (let index = 0; index < lines.length; index += 1) {
|
|
@@ -920,7 +1075,7 @@ function extractOpenCodeError(stderr) {
|
|
|
920
1075
|
}
|
|
921
1076
|
return stderr.slice(0, 1e3);
|
|
922
1077
|
}
|
|
923
|
-
var ANSI_PATTERN2, STDERR_ERROR_PATTERNS, OpenCodeProvider;
|
|
1078
|
+
var ANSI_PATTERN2, STDERR_ERROR_PATTERNS, openCodeConfigSchema, openCodeAgentName, openCodeDefaultSteps, openCodeInlineSystemPromptEnv, openCodeStepsEnv, agentFieldWorkerInstruction, trueEnvValues, OpenCodeProvider;
|
|
924
1079
|
var init_opencode = __esm({
|
|
925
1080
|
"src/harness/providers/opencode.ts"() {
|
|
926
1081
|
init_base();
|
|
@@ -936,6 +1091,13 @@ var init_opencode = __esm({
|
|
|
936
1091
|
/\bUnauthorized\b/,
|
|
937
1092
|
/\bAPIError\b/
|
|
938
1093
|
];
|
|
1094
|
+
openCodeConfigSchema = "https://opencode.ai/config.json";
|
|
1095
|
+
openCodeAgentName = "agentfield-harness";
|
|
1096
|
+
openCodeDefaultSteps = 500;
|
|
1097
|
+
openCodeInlineSystemPromptEnv = "AGENTFIELD_OPENCODE_INLINE_SYSTEM_PROMPT";
|
|
1098
|
+
openCodeStepsEnv = "AGENTFIELD_OPENCODE_STEPS";
|
|
1099
|
+
agentFieldWorkerInstruction = "You are an AgentField-launched worker. Complete the assigned prompt directly. Do not invoke AgentField orchestration, the `af` CLI, `swe-planner.plan`, or delegate work back to AgentField.";
|
|
1100
|
+
trueEnvValues = /* @__PURE__ */ new Set(["1", "true", "yes", "on"]);
|
|
939
1101
|
OpenCodeProvider = class {
|
|
940
1102
|
bin;
|
|
941
1103
|
constructor(binPath = "opencode") {
|
|
@@ -943,6 +1105,8 @@ var init_opencode = __esm({
|
|
|
943
1105
|
}
|
|
944
1106
|
async execute(prompt, options) {
|
|
945
1107
|
const cmd = [this.bin, "run"];
|
|
1108
|
+
const inlineSystemPrompt = inlineSystemPromptEnabled(options);
|
|
1109
|
+
cmd.push("--agent", openCodeAgentName);
|
|
946
1110
|
const root = resolveRoot(options);
|
|
947
1111
|
if (root) {
|
|
948
1112
|
cmd.push("--dir", root);
|
|
@@ -955,17 +1119,6 @@ var init_opencode = __esm({
|
|
|
955
1119
|
if (variantValue) {
|
|
956
1120
|
cmd.push("--variant", variantValue);
|
|
957
1121
|
}
|
|
958
|
-
let effectivePrompt = prompt;
|
|
959
|
-
if (options.system_prompt && typeof options.system_prompt === "string" && options.system_prompt.trim()) {
|
|
960
|
-
effectivePrompt = `SYSTEM INSTRUCTIONS:
|
|
961
|
-
${options.system_prompt.trim()}
|
|
962
|
-
|
|
963
|
-
---
|
|
964
|
-
|
|
965
|
-
USER REQUEST:
|
|
966
|
-
${prompt}`;
|
|
967
|
-
}
|
|
968
|
-
cmd.push(effectivePrompt);
|
|
969
1122
|
if (modelValue && isOpenRouterRequest({ model: modelValue }) && !env.OPENCODE_CONFIG_CONTENT && !process.env.OPENCODE_CONFIG_CONTENT) {
|
|
970
1123
|
const modelSlug = modelValue.slice("openrouter/".length);
|
|
971
1124
|
const headers = openRouterAttributionHeaders({ env: { ...process.env, ...env } });
|
|
@@ -981,6 +1134,14 @@ ${prompt}`;
|
|
|
981
1134
|
});
|
|
982
1135
|
}
|
|
983
1136
|
}
|
|
1137
|
+
const existingConfig = Object.prototype.hasOwnProperty.call(env, "OPENCODE_CONFIG_CONTENT") ? env.OPENCODE_CONFIG_CONTENT : process.env.OPENCODE_CONFIG_CONTENT;
|
|
1138
|
+
env.OPENCODE_CONFIG_CONTENT = mergeOpenCodeConfigContent(
|
|
1139
|
+
existingConfig,
|
|
1140
|
+
buildOpenCodeConfigContent(options, modelValue, variantValue, !inlineSystemPrompt),
|
|
1141
|
+
inlineSystemPrompt
|
|
1142
|
+
);
|
|
1143
|
+
const effectivePrompt = inlineSystemPrompt ? inlineOpenCodePrompt(prompt, options) : prompt;
|
|
1144
|
+
cmd.push(effectivePrompt);
|
|
984
1145
|
const startApi = Date.now();
|
|
985
1146
|
try {
|
|
986
1147
|
const { stdout, stderr, exitCode } = await runCli(cmd, { env });
|