@max10240/opencode-plugin-god 0.0.0-stage → 1.0.30

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 CHANGED
@@ -1,3 +1,95 @@
1
- # Temporary Holding Version
1
+ # God agent
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Adds a `god` agent. When `god` handles a request, its system prompt is replaced with the prompt configured for the current model ID. Every other agent keeps its normal system prompt.
4
+
5
+ The plugin ships no prompt content. You provide the files or URLs.
6
+
7
+ ## Install
8
+
9
+ Standalone:
10
+
11
+ ```jsonc
12
+ // opencode.jsonc
13
+ {
14
+ "$schema": "https://opencode.ai/config.json",
15
+ "plugins": [
16
+ {
17
+ "package": "@max10240/opencode-plugin-god",
18
+ "options": {
19
+ "models": {
20
+ "*": "~/.config/opencode/god/default.md",
21
+ "deepseek-v4.1-flash": "~/.config/opencode/god/deepseek-v4.1-flash.md"
22
+ }
23
+ }
24
+ }
25
+ ]
26
+ }
27
+ ```
28
+
29
+ Through Unicorn:
30
+
31
+ ```jsonc
32
+ {
33
+ "plugins": [
34
+ {
35
+ "package": "@max10240/opencode-plugin-unicorn",
36
+ "options": {
37
+ "features": { "god": true },
38
+ "god": {
39
+ "models": {
40
+ "deepseek-v4.1-flash": "~/.config/opencode/god/deepseek-v4.1-flash.md"
41
+ }
42
+ }
43
+ }
44
+ }
45
+ ]
46
+ }
47
+ ```
48
+
49
+ Enable it with `features.god: true` or by providing a `god` options object. `features.god: false` disables it.
50
+
51
+ ## Options
52
+
53
+ | Option | Type | Default | Description |
54
+ | --- | --- | --- | --- |
55
+ | `models` | object | `{}` | Model ID to prompt source mapping |
56
+ | `cache` | boolean | `true` | Cache loaded prompts |
57
+ | `cacheTtlMs` | number | `60000` | HTTP cache lifetime |
58
+ | `timeoutMs` | number | `10000` | HTTP request timeout |
59
+
60
+ See [config.example.jsonc](https://github.com/Max10240/opencode-plugin-unicorn/blob/main/packages/opencode-plugin-god/config.example.jsonc) for a complete example.
61
+
62
+ ## Model matching
63
+
64
+ - Keys are matched against the model ID only. The provider is ignored, so the same model served by two providers uses the same prompt.
65
+ - An exact model ID wins over the `"*"` fallback.
66
+ - Without an exact match or `"*"`, the request fails with an error.
67
+ - Provider-prefixed keys such as `"openai/model-id"` are ordinary model IDs and do not match `model-id`.
68
+
69
+ ## Prompt sources
70
+
71
+ - `https://` or `http://` URL
72
+ - `file://` URL
73
+ - Absolute local path
74
+ - `~/` path
75
+ - Relative path, resolved against the workspace directory
76
+
77
+ Local files are revalidated on each request by modification time and size. HTTP responses are cached for `cacheTtlMs`. Set `cache` to `false` to load every request.
78
+
79
+ If a source cannot be loaded, is not a file, or contains only whitespace, the request fails with an error.
80
+
81
+ The `god` agent does not fall back to the default system prompt. Configure every model you use with it, or add a `"*"` entry.
82
+
83
+ ## Notes
84
+
85
+ - Select the `god` agent explicitly; other agents are never modified.
86
+ - The plugin only reads text. Prompt content and provider terms are your responsibility.
87
+ - Use either the standalone plugin or Unicorn, not both at the same time.
88
+
89
+ ## Development
90
+
91
+ ```bash
92
+ bun test
93
+ bun run typecheck
94
+ bun run build
95
+ ```
@@ -0,0 +1,95 @@
1
+ # God agent
2
+
3
+ 新增一个名为 `god` 的 agent。只有使用 `god` 时,才会按当前 model ID 替换 system prompt;其他 agent 不受影响。
4
+
5
+ 插件本身不包含任何提示词内容,文件或 URL 由你提供。
6
+
7
+ ## 安装
8
+
9
+ 单独使用:
10
+
11
+ ```jsonc
12
+ // opencode.jsonc
13
+ {
14
+ "$schema": "https://opencode.ai/config.json",
15
+ "plugins": [
16
+ {
17
+ "package": "@max10240/opencode-plugin-god",
18
+ "options": {
19
+ "models": {
20
+ "*": "~/.config/opencode/god/default.md",
21
+ "deepseek-v4.1-flash": "~/.config/opencode/god/deepseek-v4.1-flash.md"
22
+ }
23
+ }
24
+ }
25
+ ]
26
+ }
27
+ ```
28
+
29
+ 通过 Unicorn 使用:
30
+
31
+ ```jsonc
32
+ {
33
+ "plugins": [
34
+ {
35
+ "package": "@max10240/opencode-plugin-unicorn",
36
+ "options": {
37
+ "features": { "god": true },
38
+ "god": {
39
+ "models": {
40
+ "deepseek-v4.1-flash": "~/.config/opencode/god/deepseek-v4.1-flash.md"
41
+ }
42
+ }
43
+ }
44
+ }
45
+ ]
46
+ }
47
+ ```
48
+
49
+ 使用 Unicorn 时,通过 `features.god: true` 或提供 `god` 配置对象来启用;`features.god: false` 会关闭。
50
+
51
+ ## 选项
52
+
53
+ | 选项 | 类型 | 默认值 | 说明 |
54
+ | --- | --- | --- | --- |
55
+ | `models` | object | `{}` | model ID 到提示词来源的映射 |
56
+ | `cache` | boolean | `true` | 是否缓存已加载的提示词 |
57
+ | `cacheTtlMs` | number | `60000` | HTTP 缓存时间 |
58
+ | `timeoutMs` | number | `10000` | HTTP 请求超时 |
59
+
60
+ 完整示例见 [config.example.jsonc](https://github.com/Max10240/opencode-plugin-unicorn/blob/main/packages/opencode-plugin-god/config.example.jsonc)。
61
+
62
+ ## 模型匹配
63
+
64
+ - 只按 model ID 匹配,provider 不参与,因此同一模型由不同 provider 提供时使用同一个提示词。
65
+ - 精确 model ID 优先于 `"*"` 回退项。
66
+ - 没有精确匹配也没有 `"*"` 时,请求直接报错。
67
+ - `"openai/model-id"` 这类 provider 前缀只是普通 model ID,不会匹配 `model-id`。
68
+
69
+ ## 提示词来源
70
+
71
+ - `https://` 或 `http://` URL
72
+ - `file://` URL
73
+ - 绝对路径
74
+ - `~/` 路径
75
+ - 相对路径,以工作区目录为基准
76
+
77
+ 本地文件每次请求都会按修改时间和大小重新校验。HTTP 响应在 `cacheTtlMs` 内缓存。将 `cache` 设为 `false` 可每次重新加载。
78
+
79
+ 加载失败、不是文件、或内容只有空白时,请求直接报错。
80
+
81
+ `god` 不会回退到默认 system prompt。请为每个会使用的模型配置映射,或添加 `"*"` 回退项。
82
+
83
+ ## 说明
84
+
85
+ - 需要显式选择 `god` agent;其他 agent 永远不会被修改。
86
+ - 插件只读取文本;提示词内容和 provider 条款由你负责。
87
+ - 单独插件和 Unicorn 二选一,不要同时启用。
88
+
89
+ ## 开发
90
+
91
+ ```bash
92
+ bun test
93
+ bun run typecheck
94
+ bun run build
95
+ ```
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default, setupGodAgent } from "./src/index.js";
@@ -0,0 +1,9 @@
1
+ import { Plugin } from "@opencode/plugin";
2
+ import { setupGodAgent } from "./setup.js";
3
+ export default Plugin.define({
4
+ id: "max10240.god",
5
+ setup: setupGodAgent,
6
+ });
7
+ export { GOD_AGENT_ID, parseGodOptions, selectPromptSource } from "./options.js";
8
+ export { PromptLoader, expandHome, parseSource } from "./prompt.js";
9
+ export { setupGodAgent } from "./setup.js";
@@ -0,0 +1,45 @@
1
+ export const GOD_AGENT_ID = "god";
2
+ export const FALLBACK_MODEL_KEY = "*";
3
+ export const DEFAULT_CACHE_TTL_MS = 60_000;
4
+ export const DEFAULT_TIMEOUT_MS = 10_000;
5
+ /**
6
+ * Reads the plugin options. Unknown fields are ignored so the plugin can
7
+ * share an options object with the rest of the Unicorn bundle.
8
+ */
9
+ export function parseGodOptions(value) {
10
+ const source = isRecord(value) ? value : {};
11
+ const models = new Map();
12
+ if (isRecord(source.models)) {
13
+ for (const [rawModel, rawPrompt] of Object.entries(source.models)) {
14
+ if (typeof rawPrompt !== "string")
15
+ continue;
16
+ const model = rawModel.trim();
17
+ const prompt = rawPrompt.trim();
18
+ if (!model || !prompt)
19
+ continue;
20
+ models.set(model, prompt);
21
+ }
22
+ }
23
+ return {
24
+ models,
25
+ cache: typeof source.cache === "boolean" ? source.cache : true,
26
+ cacheTtlMs: positiveNumber(source.cacheTtlMs) ?? DEFAULT_CACHE_TTL_MS,
27
+ timeoutMs: positiveNumber(source.timeoutMs) ?? DEFAULT_TIMEOUT_MS,
28
+ };
29
+ }
30
+ /**
31
+ * Selects a prompt source for a model ID. The provider is deliberately not
32
+ * part of the key: the same model served by two providers maps to one prompt.
33
+ */
34
+ export function selectPromptSource(options, modelID) {
35
+ const exact = options.models.get(modelID);
36
+ if (exact)
37
+ return exact;
38
+ return options.models.get(FALLBACK_MODEL_KEY);
39
+ }
40
+ function positiveNumber(value) {
41
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
42
+ }
43
+ function isRecord(value) {
44
+ return typeof value === "object" && value !== null && !Array.isArray(value);
45
+ }
@@ -0,0 +1,90 @@
1
+ import { readFile, stat } from "node:fs/promises";
2
+ import { homedir } from "node:os";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ /**
6
+ * Loads prompt text from a local path or an HTTP(S) URL and keeps a small
7
+ * cache. Local files are revalidated with stat; remote files use a TTL.
8
+ */
9
+ export class PromptLoader {
10
+ options;
11
+ local = new Map();
12
+ remote = new Map();
13
+ constructor(options) {
14
+ this.options = options;
15
+ }
16
+ async load(source) {
17
+ try {
18
+ const target = parseSource(source, this.options.directory, this.options.home);
19
+ if (target.kind === "http")
20
+ return await this.loadRemote(source, target.url);
21
+ return await this.loadLocal(source, target.filepath);
22
+ }
23
+ catch (error) {
24
+ return { source, error: error instanceof Error ? error.message : String(error) };
25
+ }
26
+ }
27
+ async loadLocal(source, filepath) {
28
+ const info = await stat(filepath);
29
+ if (!info.isFile())
30
+ return { source, error: `not a file: ${filepath}` };
31
+ if (this.options.cache) {
32
+ const cached = this.local.get(filepath);
33
+ if (cached && cached.mtimeMs === info.mtimeMs && cached.size === info.size) {
34
+ return { source, text: cached.text };
35
+ }
36
+ }
37
+ const text = await readFile(filepath, "utf8");
38
+ if (this.options.cache) {
39
+ this.local.set(filepath, { text, mtimeMs: info.mtimeMs, size: info.size });
40
+ }
41
+ return { source, text };
42
+ }
43
+ async loadRemote(source, url) {
44
+ if (this.options.cache) {
45
+ const cached = this.remote.get(url);
46
+ if (cached && Date.now() - cached.at < this.options.cacheTtlMs) {
47
+ return { source, text: cached.text };
48
+ }
49
+ }
50
+ const fetchImpl = this.options.fetchImpl ?? fetch;
51
+ const response = await fetchImpl(url, { signal: AbortSignal.timeout(this.options.timeoutMs) });
52
+ if (!response.ok)
53
+ return { source, error: `HTTP ${response.status} ${response.statusText}`.trim() };
54
+ const text = await response.text();
55
+ if (this.options.cache)
56
+ this.remote.set(url, { text, at: Date.now() });
57
+ return { source, text };
58
+ }
59
+ }
60
+ /**
61
+ * Supports HTTP(S) URLs, file URLs, absolute paths, `~/` paths, and paths
62
+ * relative to the workspace directory.
63
+ */
64
+ export function parseSource(source, directory, home = homedir()) {
65
+ const value = source.trim();
66
+ const url = parseUrl(value);
67
+ if (url) {
68
+ if (url.protocol === "http:" || url.protocol === "https:")
69
+ return { kind: "http", url: url.toString() };
70
+ if (url.protocol === "file:")
71
+ return { kind: "file", filepath: fileURLToPath(url) };
72
+ }
73
+ const expanded = expandHome(value, home);
74
+ return { kind: "file", filepath: path.isAbsolute(expanded) ? expanded : path.resolve(directory, expanded) };
75
+ }
76
+ export function expandHome(source, home = homedir()) {
77
+ if (source === "~")
78
+ return home;
79
+ if (source.startsWith("~/") || source.startsWith("~\\"))
80
+ return path.join(home, source.slice(2));
81
+ return source;
82
+ }
83
+ function parseUrl(source) {
84
+ try {
85
+ return new URL(source);
86
+ }
87
+ catch {
88
+ return undefined;
89
+ }
90
+ }
@@ -0,0 +1,52 @@
1
+ import { GOD_AGENT_ID, parseGodOptions, selectPromptSource } from "./options.js";
2
+ import { PromptLoader } from "./prompt.js";
3
+ const GOD_AGENT_DESCRIPTION = "Uses the model-specific system prompt configured for this agent.";
4
+ export async function setupGodAgent(context) {
5
+ const options = parseGodOptions(context.options);
6
+ const loader = new PromptLoader({
7
+ directory: context.location?.directory ?? process.cwd(),
8
+ cache: options.cache,
9
+ cacheTtlMs: options.cacheTtlMs,
10
+ timeoutMs: options.timeoutMs,
11
+ });
12
+ const agentRegistration = await context.agent.transform((editor) => {
13
+ const exists = editor.get(GOD_AGENT_ID) !== undefined;
14
+ editor.update(GOD_AGENT_ID, (agent) => {
15
+ if (exists)
16
+ return;
17
+ agent.description = GOD_AGENT_DESCRIPTION;
18
+ agent.mode = "primary";
19
+ });
20
+ });
21
+ const hookRegistration = await context.session
22
+ .hook("context", async (input) => {
23
+ if (String(input.agent) !== GOD_AGENT_ID)
24
+ return;
25
+ const modelID = String(input.model.id);
26
+ const source = selectPromptSource(options, modelID);
27
+ if (!source) {
28
+ throw new Error(`[god] no system prompt configured for model "${modelID}"`);
29
+ }
30
+ const result = await loader.load(source);
31
+ if (result.error) {
32
+ throw new Error(`[god] failed to load system prompt for model "${modelID}" from ${source}: ${result.error}`);
33
+ }
34
+ const text = result.text ?? "";
35
+ if (!text.trim()) {
36
+ throw new Error(`[god] system prompt for model "${modelID}" is empty: ${source}`);
37
+ }
38
+ input.system.splice(0, input.system.length, { type: "text", text });
39
+ })
40
+ .catch(async (error) => {
41
+ await agentRegistration.dispose();
42
+ throw error;
43
+ });
44
+ return async () => {
45
+ try {
46
+ await hookRegistration.dispose();
47
+ }
48
+ finally {
49
+ await agentRegistration.dispose();
50
+ }
51
+ };
52
+ }
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package.json",
2
3
  "name": "@max10240/opencode-plugin-god",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
4
+ "version": "1.0.30",
5
+ "description": "Adds a god agent whose system prompt is selected by model ID",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./dist/index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "bun test test/*.test.ts",
12
+ "typecheck": "tsc --noEmit",
13
+ "build": "tsc -p tsconfig.build.json",
14
+ "prepack": "node ../../scripts/sync-recommended-rules.mjs && npm run build"
15
+ },
16
+ "files": [
17
+ "dist/**/*.js",
18
+ "README.md",
19
+ "README.zh-CN.md"
20
+ ],
21
+ "keywords": [
22
+ "opencode",
23
+ "opencode-plugin",
24
+ "agent",
25
+ "system-prompt"
26
+ ],
27
+ "license": "MIT",
28
+ "peerDependencies": {
29
+ "@opencode/plugin": "^2.2.7"
30
+ },
31
+ "devDependencies": {
32
+ "@types/bun": "^1.3.0",
33
+ "@types/node": "^22.0.0",
34
+ "typescript": "^5.8.2",
35
+ "@opencode/plugin": "^2.2.7"
36
+ },
37
+ "publishConfig": {
38
+ "registry": "https://registry.npmjs.org/",
39
+ "access": "public"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/Max10240/opencode-plugin-unicorn.git",
44
+ "directory": "packages/opencode-plugin-god"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "@opencode/plugin": {
48
+ "optional": true
49
+ }
50
+ }
51
+ }