feat(vikunja): Vikunja 任务管理插件(28 工具)

token 走 password+Secret 配置项、每次调用前 ensure() 重读(换 token 无需重启);
v1/v2 差异在插件内处理(建任务 PUT/POST、改任务整对象/PATCH、搜索 ?s=/?q=、
标签对象/{label_id}、TimeEntry 无 seconds 语义);16 项单测 + 沙盒 homed 实测 28 工具全注册。
This commit is contained in:
JianFeeeee
2026-09-12 23:16:33 +08:00
parent ebd700eaf9
commit 4cf2df5be6
5 changed files with 3041 additions and 0 deletions

81
example/vikunja/README.md Normal file
View File

@ -0,0 +1,81 @@
# Vikunja 插件HomeAgent
把 [Vikunja](https://vikunja.io) 待办/任务管理接入 HomeAgent用自然语言查任务、建任务、改期、完成、看板拖动、指派、评论、时间跟踪、导入数据等。
## 配置项(全部可在插件配置界面修改)
| 键 | 类型 | 默认 | 说明 |
|---|---|---|---|
| `url` | string | `https://vikunja.jianfgit.xyz` | 站点根地址,**不带** `/api` |
| `token` | password(secret) | 空 | **必填**。Vikunja → 设置 → API Tokens 生成(`tk_` 开头)。令牌的权限范围决定本插件能力上限:勾全范围即为完整能力 |
| `api_version` | select | `v2` | `v2`(推荐,标准 REST含时间跟踪等新能力`v1`(用于 v2 暂未提供的端点) |
| `default_project_id` | string | 空 | 新建任务未指定项目时落到这里;留空则必须显式指定 |
| `max_items` | int | `25` | 列表类工具的默认条数,控制上下文体积 |
| `compact_output` | bool | `true` | 任务/项目/标签列表只返回关键字段;关闭则返回 Vikunja 完整对象 |
| `timeout_seconds` | int | `20` | 单次 HTTP 超时 |
| `verify_tls` | bool | `true` | 自签证书站点可关闭(不建议) |
配置在**每次工具调用前重新读取**,因此换了 token 不必重启插件。
## 工具
| 工具 | 能力 |
|---|---|
| `vikunja_status` | 连接/配置自检地址、token 对应的用户、API 版本、服务器能力、CalDAV 地址 |
| `vikunja_tasks` | 列任务按项目、完成状态、截止today/this_week/overdue/no_due、关键词、原生 filter 表达式 |
| `vikunja_task_get` / `task_create` / `task_update` / `task_done` / `task_delete` | 任务增删改查(`task_update` 只传要改的字段) |
| `vikunja_task_bulk` | 批量改完成状态/项目/优先级/截止/标签 |
| `vikunja_task_assignees` / `task_labels` / `task_comments` / `task_relations` / `task_attachments` | 指派、标签、评论、关联(子任务/依赖/相关)、附件(支持上传本地文件) |
| `vikunja_projects` / `project_views` | 项目增删改查、归档;视图与看板桶(把任务移入桶=看板拖动) |
| `vikunja_labels` / `filters` | 标签、保存的筛选器Saved Filter |
| `vikunja_teams` / `sharing` | 团队与成员;项目分享(用户/团队授权、链接分享含密码) |
| `vikunja_notifications` / `subscriptions` / `webhooks` | 通知、订阅、Webhook 管理 |
| `vikunja_time_entries` | 时间跟踪(**仅 v2**):补录/修改/删除、开始与停止计时器 |
| `vikunja_migrate` | 从 TickTick/WeKan/CSV/Planka/Vikunja 文件v2与 Todoist/Trello/微软待办v1导入 |
| `vikunja_user` / `vikunja_admin` | 当前账号设置、登录会话、API Token与实例管理用户增删/提权/停用/改密、项目归属转移,需实例管理员) |
| `vikunja_reactions` | 任务/评论的表情回应 |
| `vikunja_api` | **通用直通**:调任意端点,未封装的能力走这里(可强制指定 v1/v2保证能力无死角 |
## v1 / v2 差异(已按实例自带规范逐条核对)
插件默认 v2并自动处理下列差异
| 操作 | v1 | v2 |
|---|---|---|
| 建任务 | `PUT /projects/{id}/tasks` | `POST /projects/{id}/tasks` |
| 改任务 | `POST /tasks/{id}`(必须整对象 → 插件自动取回-合并-提交) | `PATCH /tasks/{id}`merge-patch只发变更字段被拒则回落取回-合并-PUT |
| 搜索参数 | `?s=` | `?q=` |
| 加标签 | `PUT`Label 对象) | `POST``{"label_id":N}` |
| 批量改 | `POST /tasks/bulk` | `PUT /tasks/bulk` |
| 时间跟踪 | 不支持 | `/time-entries``end_time` 为 null 即计时中;停止用 `/time-entries/timer/stop` |
| 导入 | Todoist / Trello / 微软待办 | TickTick / WeKan / CSV / Planka / Vikunja 文件 |
> 官方路线v1 仍支持但新端点只进 v23.0 弃用、4.0 移除。除“导入”外建议一律用 v2。
## 开发与构建
```bash
cd third_party/homeagent-sdk/example/vikunja
go test -count=1 -race ./... # 16 项测试httptest 打桩,不需要真 token
hmapdev build # 产出 dist/vikunja_bundle.hmap
```
`go.mod` 里的 `replace` 把 SDK 指向仓库内的 `third_party/homeagent-sdk`,因此无需联网拉私有模块。
### 部署到运行实例
`.hmap` 包内是 `plugin.json` + `plugin.bin.<os>.<arch>`,安装时按运行平台重命名入口文件:
```bash
unzip -o dist/vikunja_bundle.hmap -d /home/newqqagent/plugins/vikunja
cd /home/newqqagent/plugins/vikunja && mv plugin.bin.linux.amd64 plugin.bin
# 然后重载插件(或重启 homeagent.service
```
## 已知边界
- **附件下载**未单独封装:`task_attachments` 支持列出/上传/删除,下载请用 `vikunja_api` 访问附件 URL。
- **链接分享的字段**`right`/`password`)按 Vikunja 版本语义透传;如遇 4xx可直接用 `raw` 参数传完整 JSON。
- **批量改标签**的 `fields` 结构以 `BulkTask` 为准,未在真实实例上验证过(缺少可用 token如有偏差请用 `vikunja_api` 直通。
- CalDAV 是客户端协议,插件只提供地址(`vikunja_status` 里的 `caldav_url`),不做 CalDAV 同步。

11
example/vikunja/go.mod Normal file
View File

@ -0,0 +1,11 @@
module vikunja-plugin
go 1.25.0
require gitcode.com/JianFeeeee/homeagent-sdk v1.2.0
// 与同目录其它示例一致SDK 指向仓库内的 vendored 副本
replace gitcode.com/JianFeeeee/homeagent-sdk => /root/.homeagent/hmapdev/sdk/v1.2.0

12
example/vikunja/plg.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "vikunja",
"name_zh": "Vikunja 待办",
"name_en": "Vikunja",
"version": "1.0.0",
"description": "Vikunja 待办/任务管理任务增删改查、项目与看板桶、标签、指派、评论、关联、附件、保存筛选器、团队与分享、通知、订阅、Webhook、时间跟踪、数据导入、实例管理并附通用 API 直通工具兜底",
"author": "HomeAgent",
"entry": "plugin.bin",
"sdk": "1.2.0",
"tags": ["vikunja", "todo", "task", "gtd", "productivity"],
"targets": "linux/amd64"
}

2534
example/vikunja/plugin.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,403 @@
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// newTestPlugin 构造一个不依赖 sdk 的插件实例,指向 httptest 服务。
// ensure() 在 sdk==nil 时会保留已设置的字段,因此可以这样直接测处理器。
func newTestPlugin(t *testing.T, h http.HandlerFunc) (*Plugin, *httptest.Server) {
t.Helper()
srv := httptest.NewServer(h)
t.Cleanup(srv.Close)
p := &Plugin{
name: "vikunja",
baseURL: srv.URL,
token: "tk_test",
apiVer: "v2",
maxItems: 5,
compact: true,
http: srv.Client(),
}
return p, srv
}
func mustJSON(t *testing.T, v interface{}) []byte {
t.Helper()
b, err := json.Marshal(v)
if err != nil {
t.Fatalf("marshal: %v", err)
}
return b
}
// 1) 列表v2 用 q= 搜索,且 filter 会带上默认 done 条件
func TestTasksListV2(t *testing.T) {
var gotQuery string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
gotQuery = r.URL.RawQuery
if r.Header.Get("Authorization") != "Bearer tk_test" {
t.Errorf("缺少 Bearer 头: %q", r.Header.Get("Authorization"))
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[{"id":1,"title":"写周报","done":false,"project_id":3,"due_date":"2026-09-13T10:00:00Z","labels":[{"title":"工作"}],"assignees":[{"username":"jianf"}]}]`))
})
res, err := p.handleTasksList(map[string]interface{}{"search": "周报", "limit": float64(5)})
if err != nil {
t.Fatalf("err: %v", err)
}
if !strings.Contains(gotQuery, "q=%E5%91%A8%E6%8A%A5") {
t.Errorf("v2 应使用 q= 搜索,实际 query=%s", gotQuery)
}
if strings.Contains(gotQuery, "s=") {
t.Errorf("v2 不应使用 s=,实际 query=%s", gotQuery)
}
if !strings.Contains(gotQuery, "per_page=5") {
t.Errorf("per_page 未生效: %s", gotQuery)
}
if !strings.Contains(gotQuery, "filter=done+%3D+false") && !strings.Contains(gotQuery, "filter=done%20%3D%20false") {
t.Errorf("默认应过滤未完成,实际 filter 片段: %s", gotQuery)
}
m, ok := res.(map[string]interface{})
if !ok {
t.Fatalf("结果应为 map实际 %T", res)
}
if m["count"].(int) != 1 {
t.Errorf("count 应为 1实际 %v", m["count"])
}
tasks := m["tasks"].([]interface{})
tk := tasks[0].(map[string]interface{})
if _, ok := tk["labels"].([]string); !ok {
t.Errorf("标签应被投影成名称数组,实际 %T", tk["labels"])
}
if _, ok := tk["description"]; ok {
t.Errorf("精简输出不应出现 description")
}
}
// 2) 列表v1 用 s= 搜索
func TestTasksListV1SearchParam(t *testing.T) {
var gotQuery string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
gotQuery = r.URL.RawQuery
_, _ = w.Write([]byte(`[]`))
})
p.apiVer = "v1"
if _, err := p.handleTasksList(map[string]interface{}{"search": "abc"}); err != nil {
t.Fatalf("err: %v", err)
}
if !strings.Contains(gotQuery, "s=abc") {
t.Errorf("v1 应使用 s= 搜索,实际 %s", gotQuery)
}
if strings.Contains(gotQuery, "q=") {
t.Errorf("v1 不应出现 q=,实际 %s", gotQuery)
}
}
// 3) 建任务v1=PUT、v2=POST同路径方法不同
func TestTaskCreateMethodByVersion(t *testing.T) {
for _, tc := range []struct {
ver string
method string
}{
{"v1", http.MethodPut},
{"v2", http.MethodPost},
} {
var gotMethod, gotPath string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
_, _ = w.Write([]byte(`{"id":42,"title":"买菜"}`))
})
p.apiVer = tc.ver
if _, err := p.handleTaskCreate(map[string]interface{}{"project_id": "3", "title": "买菜"}); err != nil {
t.Fatalf("[%s] err: %v", tc.ver, err)
}
if gotMethod != tc.method {
t.Errorf("[%s] 期望 %s实际 %s", tc.ver, tc.method, gotMethod)
}
if gotPath != "/api/"+tc.ver+"/projects/3/tasks" {
t.Errorf("[%s] 路径错误: %s", tc.ver, gotPath)
}
}
}
// 4) 改任务v2走 merge-patch只发变更字段
func TestTaskUpdateV2MergePatch(t *testing.T) {
var method, ctype string
var body map[string]interface{}
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
method = r.Method
ctype = r.Header.Get("Content-Type")
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &body)
_, _ = w.Write([]byte(`{"id":7,"done":true}`))
})
if _, err := p.handleTaskUpdate(map[string]interface{}{"id": "7", "done": true}); err != nil {
t.Fatalf("err: %v", err)
}
if method != http.MethodPatch {
t.Errorf("v2 应用 PATCH实际 %s", method)
}
if !strings.Contains(ctype, "merge-patch") {
t.Errorf("应使用 merge-patch 内容类型,实际 %s", ctype)
}
if len(body) != 1 || body["done"] != true {
t.Errorf("只应发送变更字段,实际 %v", body)
}
}
// 5) 改任务v2回退merge-patch 被拒 → 取回-合并-PUT
func TestTaskUpdateV2FallbackToMergePut(t *testing.T) {
var calls []string
var putBody map[string]interface{}
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
calls = append(calls, r.Method+" "+r.URL.Path)
switch {
case r.Method == http.MethodPatch:
w.WriteHeader(http.StatusUnsupportedMediaType)
_, _ = w.Write([]byte(`{"code":9,"message":"unsupported media type"}`))
case r.Method == http.MethodGet:
_, _ = w.Write([]byte(`{"id":7,"title":"旧标题","done":false,"priority":1}`))
case r.Method == http.MethodPut:
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &putBody)
_, _ = w.Write([]byte(`{"id":7,"title":"新标题","done":false,"priority":1}`))
default:
t.Errorf("意外请求: %s %s", r.Method, r.URL.Path)
}
})
if _, err := p.handleTaskUpdate(map[string]interface{}{"id": "7", "title": "新标题"}); err != nil {
t.Fatalf("err: %v", err)
}
want := []string{"PATCH /api/v2/tasks/7", "GET /api/v2/tasks/7", "PUT /api/v2/tasks/7"}
if len(calls) != len(want) {
t.Fatalf("调用序列不符: %v", calls)
}
for i := range want {
if calls[i] != want[i] {
t.Errorf("第 %d 步期望 %s实际 %s", i+1, want[i], calls[i])
}
}
if putBody["title"] != "新标题" {
t.Errorf("合并后的 body 应含新标题,实际 %v", putBody)
}
if putBody["priority"] != float64(1) {
t.Errorf("合并必须保留原有字段priority实际 %v", putBody)
}
}
// 6) 改任务v1没有 merge-patch必须取回-合并-POST
func TestTaskUpdateV1FetchMergePost(t *testing.T) {
var calls []string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
calls = append(calls, r.Method+" "+r.URL.Path)
if r.Method == http.MethodGet {
_, _ = w.Write([]byte(`{"id":9,"title":"旧","priority":2}`))
return
}
_, _ = w.Write([]byte(`{"id":9,"title":"新","priority":2}`))
})
p.apiVer = "v1"
if _, err := p.handleTaskUpdate(map[string]interface{}{"id": "9", "title": "新"}); err != nil {
t.Fatalf("err: %v", err)
}
want := []string{"GET /api/v1/tasks/9", "POST /api/v1/tasks/9"}
if len(calls) != 2 || calls[0] != want[0] || calls[1] != want[1] {
t.Fatalf("v1 应为 GET→POST实际 %v", calls)
}
}
// 7) 错误映射401 提示检查 token
func TestErrorHint401(t *testing.T) {
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"code":11,"message":"invalid token"}`))
})
_, err := p.handleTasksList(map[string]interface{}{})
if err == nil {
t.Fatal("应返回错误")
}
msg := err.Error()
if !strings.Contains(msg, "401") || !strings.Contains(msg, "code=11") {
t.Errorf("错误信息应含状态码与 Vikunja code实际 %s", msg)
}
if !strings.Contains(msg, "token") {
t.Errorf("401 应给出 token 提示,实际 %s", msg)
}
}
// 8) 未配置 token 时应给出可操作提示,而不是发出无凭据请求
func TestMissingToken(t *testing.T) {
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
t.Error("未配置 token 时不应发请求")
})
p.token = ""
_, err := p.handleTasksList(map[string]interface{}{})
if err == nil || !strings.Contains(err.Error(), "vikunja.token") {
t.Fatalf("应提示配置项名,实际 %v", err)
}
}
// 9) 导入Todoist 必须走 v1即使插件默认是 v2
func TestMigrateUsesV1ForTodoist(t *testing.T) {
var path string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
path = r.URL.Path
_, _ = w.Write([]byte(`{"ok":true}`))
})
if _, err := p.handleMigrate(map[string]interface{}{"action": "start", "source": "todoist", "code": "abc"}); err != nil {
t.Fatalf("err: %v", err)
}
if path != "/api/v1/migration/todoist/migrate" {
t.Errorf("Todoist 导入必须走 v1实际 %s", path)
}
}
// 10) 导入WeKan 走 v2
func TestMigrateUsesV2ForWekan(t *testing.T) {
var path, method string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
path, method = r.URL.Path, r.Method
_, _ = w.Write([]byte(`{"ok":true}`))
})
if _, err := p.handleMigrate(map[string]interface{}{"action": "start", "source": "wekan"}); err != nil {
t.Fatalf("err: %v", err)
}
if path != "/api/v2/migration/wekan/migrate" || method != http.MethodPost {
t.Errorf("WeKan 应走 v2 POST实际 %s %s", method, path)
}
}
// 11) 时间跟踪:秒数换算成 end_time计时开始则不带 end_time
func TestTimeEntrySecondsBecomesEndTime(t *testing.T) {
var body map[string]interface{}
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &body)
_, _ = w.Write([]byte(`{"id":1}`))
})
start := "2026-09-12T10:00:00+08:00"
if _, err := p.handleTimeEntries(map[string]interface{}{
"action": "create", "task_id": "5", "seconds": float64(600),
"start_time": start,
}); err != nil {
t.Fatalf("err: %v", err)
}
// 判据不写死字符串:按时区无关的方式比较两个时间点
sStart, err := time.Parse(time.RFC3339, start)
if err != nil {
t.Fatalf("case 自身时间写错: %v", err)
}
gotEnd, ok := body["end_time"].(string)
if !ok {
t.Fatalf("应有 end_time实际 %v", body["end_time"])
}
tEnd, err := time.Parse(time.RFC3339, gotEnd)
if err != nil {
t.Fatalf("end_time 不是 RFC3339: %q", gotEnd)
}
if diff := tEnd.Sub(sStart); diff != 10*time.Minute {
t.Errorf("end_time 应由 start_time+600s 推出,实际差值 %v", diff)
}
if _, ok := body["seconds"]; ok {
t.Errorf("TimeEntry 没有 seconds 字段,不应发送:%v", body)
}
body = nil
if _, err := p.handleTimeEntries(map[string]interface{}{"action": "timer_start", "task_id": "5"}); err != nil {
t.Fatalf("err: %v", err)
}
v, present := body["end_time"]
if !present || v != nil {
t.Errorf("计时开始应显式 end_time=nulllive timer实际 %v", body)
}
}
// 12) 时间跟踪在 v1 下应给出明确不可用提示
func TestTimeEntryUnavailableOnV1(t *testing.T) {
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {})
p.apiVer = "v1"
_, err := p.handleTimeEntries(map[string]interface{}{"action": "list"})
if err == nil || !strings.Contains(err.Error(), "v2") {
t.Fatalf("v1 下应提示改用 v2实际 %v", err)
}
}
// 13) 标签v1 收 Label 对象、v2 收 label_id
func TestLabelBodyByVersion(t *testing.T) {
var body map[string]interface{}
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(raw, &body)
_, _ = w.Write([]byte(`{}`))
})
if _, err := p.handleTaskLabels(map[string]interface{}{"action": "add", "id": "1", "label_id": "5"}); err != nil {
t.Fatalf("err: %v", err)
}
if body["label_id"] != float64(5) {
t.Errorf("v2 应发送 label_id实际 %v", body)
}
body = nil
p.apiVer = "v1"
if _, err := p.handleTaskLabels(map[string]interface{}{"action": "add", "id": "1", "label_id": "5"}); err != nil {
t.Fatalf("err: %v", err)
}
if body["id"] != float64(5) {
t.Errorf("v1 应发送 Label 对象(id),实际 %v", body)
}
}
// 14) 时间字符串容忍today / +3d / ISO
func TestNormalizeTime(t *testing.T) {
for _, in := range []string{"today", "tomorrow", "+3d", "2026-09-12 18:00", "2026-09-12T18:00:00+08:00"} {
got := normalizeTime(in)
s, ok := got.(string)
if !ok {
t.Fatalf("%s: 期望字符串,实际 %T", in, got)
}
if _, err := time.Parse(time.RFC3339, s); err != nil {
t.Errorf("%s → %s 不是 RFC3339: %v", in, s, err)
}
}
}
// 15) 通用直通:可指定 api_versionmethod 大小写不敏感
func TestRawAPI(t *testing.T) {
var method, path string
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
method, path = r.Method, r.URL.Path
_, _ = w.Write([]byte(`[]`))
})
if _, err := p.handleRawAPI(map[string]interface{}{"method": "get", "path": "projects", "api_version": "v1"}); err != nil {
t.Fatalf("err: %v", err)
}
if method != http.MethodGet || path != "/api/v1/projects" {
t.Errorf("直通参数未生效: %s %s", method, path)
}
}
// 16) 精简输出可关闭(关闭时返回原样)
func TestCompactToggle(t *testing.T) {
p, _ := newTestPlugin(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`[{"id":1,"title":"t","description":"很长的描述","done":false}]`))
})
p.compact = false
res, err := p.handleTasksList(map[string]interface{}{})
if err != nil {
t.Fatalf("err: %v", err)
}
arr, ok := res.([]interface{})
if !ok {
t.Fatalf("关闭精简后应原样返回数组,实际 %T", res)
}
if _, ok := arr[0].(map[string]interface{})["description"]; !ok {
t.Errorf("关闭精简后应保留 description")
}
}